C++ Programming
- Write a C++ Program to print a welcome text in a separate line.
- Write a Program in C++ to add two numbers.
- C++ program for addition of two numbers using functions.
- Add two numbers in C++ using class.
- Addition of two numbers using call by reference in C++
- C++ program to write a program to add two numbers using call by value.
- Swapping of two numbers in C++ without using third variable.
- Swap two numbers in C++ using function.
- C++ program to swap two numbers using class.
- C++ program to swap two numbers using call by value.
- C++ program to swap two numbers using pointers.
- Write a program to swap private data members of two classes using friend function.
- write a program to add data objects of two different classes using friend function.
- Threading in C++.
- C++ thread lambda
- C++ thread lambda with parameter
- C++ thread lambda pass by reference
- Singleton C++ example
Write a program in C++ to check whether a given string is a palindrome or not using the pointer method.
Method 1
#include <iostream>
#include <cstring>
using namespace std;
//Function to check if a string is palindrome
bool isPalindrome(const char* str)
{
//Pointer at the start of the string
const char* left = str;
//Pointer at the end of the string
const char* right = str + strlen(str) - 1;
while (left < right)
{
if (*left != *right)
{
return false; // Mismatch found
}
left++; // Move left pointer forward
right--; // Move right pointer backward
}
//Return true if we get palindrome
return true;
}
int main() {
char str[200];
cout << "Enter any string: ";
cin.getline(str, 200);
if (isPalindrome(str))
{
cout <<str << " is a palindrome" << endl;
} else
{
cout <<str << "is not a palindrome." << endl;
}
return 0;
}
Output 1
Enter any string: radar
radar is a palindrome
Output 2
Enter any string: hello
hellois not a palindrome.
Method 2
#include <iostream>
#include <string>
using namespace std;
//Function to check if the given string is palindrome or not
bool isPalindrome(const string& str)
{
int left = 0;
int right = str.length() - 1;
while (left < right)
{
if (str[left] != str[right])
{
return false;
}
left++;
right--;
}
return true;
}
int main() {
string str;
cout << "Enter any string: ";
getline(cin, str);
if (isPalindrome(str))
{
cout << str << " is a palindrome" << endl;
} else {
cout << str << " is not a palindrome" << endl;
}
return 0;
}
Output 1
Enter any string: radar
radar is a palindrome
Output 2
Enter any string: hello
hello is not a palindrome