In this program, you'll learn how to check string is palindrome or not.
The basic logic is that you have to find the reverse string and compare it with original string. If both are equal the string is Palindrome otherwise not.
We have used three string functions which are: strcpy(), strrev() and strcmp()
The strcpy() is the C library function copies the string pointed by source (including the null character) to the destination. The strcpy() function also returns the copied string.
The function definition of strcpy() is: char *strcpy(const char *dest, const char *src)
The strrev() is the C library function which reverses a given string.
The function definition of strrev() is: char *strrev(const char *str);
The strcmp() is the C library function compares the string pointed to, by str1 to the string pointed to by str2.
The function definition of strcmp() is: int strcmp(const char *str1, const char *str2)
For example,
str = "nitin"
If you take the reverse of this string, then you will get "nitin" which same as original string, means it is Palindrome string.
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str1[20],str2[20];
cout<<"Enter string which to be checked: ";
cin>> str1;
strcpy(str2, str1);
strrev(str2);
if(strcmp(str1, str2) == 0)
cout<<"String is Palindrome ";
else
cout<<"String is not Palindrome ";
return 0;
}
Enter string which to be checked: nitin
string is Palindrome
Enter string which to be checked: abca
String is not Palindrome