The logic for mine (that works for me) is:
get string
get length of string
loop for half the length of string
if the character at position "c" & position of length-c are the same, keep looping, otherwise it's not a Palindrome
#include<iostream>
#include<string>
usingnamespace std; // the namespace for cout<< & such functions
int main()
{
char strn[80];
cout<<"Enter the string: ";
cin.getline(strn,80);
int len=strlen(strn);
bool flag=true; // create a Boolean value, "flag" to be used in our loop
for(int c=0;c!=len/2;c++) // do a loop from 0 to half the length of the string
{
if(flag) // if it is a palindrome so far
{
if(strn[c]!=strn[len-c-1]) // check the characters match
{
flag=false; // if they don't set the indicator to false
}
}
else
{
break; // if it is not a palindrome, exit the for loop
}
}
// if flag is true cout "Palindrome" otherwise output "Not Palindrome"
if(flag)
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
cin.get();
return 0;
}
char *s = strn; // point to first char
char *e = strn + strlen(strn) - 1; // point to last char
while ( *s == *e && s != e ) ++s, --e; // move towards each other
bool flag = ( s == e );
if ( !flag ) cout << " Not a ";
cout << "Palindrome" << endl;