I am writing a program that will check whether or not specific text input is a palindrome or not.
I'm a little stuck trying to fit in my loop while checking the text.
Any help would be greatly appreciated.
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
char strn[80];
cout<<"Please enter your text input: ";
cin.getline(strn,80);
int len=strlen(strn);
int loop=0;
bool palin=true;
for(int c=0;c!=len/2;c++)
{
if(palin)
{
if(strn[c]!=strn[len-c-1])
{
palin=false;
}
}
else
{
break;
}
}
if(palin)
{
cout<<"Your text input is a palindrome.";
}
else
{
cout<<"Your text input is not Palindrome.";
}
cin.get();
return 0;
}
It might be simpler to use std::string and std::reverse. You could copy the string, reverse it and check for equality. It might be less efficient but would be much simpler. there is a getline that reads into a std::string.
I'm not sure what your specific problem is but I don't have time to debug it now. It seems like a fairly simply problem to debug if you just step line by line and analyze the state of the variables. Try that if you would like to make it work with the for loop. I wonder what happens to your program if there are an odd number of characters.