Question:
palindrome program in c++.?
2008-03-07 13:45:18 UTC
help writing this palindrome program for my c++ class.
HERE WHAT THE CODE SHOULD BE LIKE.
Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome. Your program should ignore blanks, punctuation, and lettercase

Your program should output what the user entered as well as the reversed line.

Sample output:

Please enter a phrase, and I will tell you if it is a palindrome.
>> Madam I'm Adam
You entered Madam I'm Adam
True. This IS a palindrome, the reversed string is madamimadam
Would you like to enter another phrase (y/n)
>> y
Please enter a phrase, and I will tell you if it is a palindrome.
>> Kayak
You entered Kayak
True. This IS a palindrome, the reversed string is kayak
Would you like to enter another phrase (y/n)
>> y
Please enter a phrase, and I will tell you if it is a palindrome.
>> Last Stall
You entered Last Stall
False. This is NOT a palindrome, the reversed string is llatstsal.

Thanks in Advance :)
Three answers:
2008-03-07 14:02:38 UTC
Hi,

sounds like an homework or an interview question :D, anyway you can use this :) :



string Test;

cin >> Test;

bool bFlag = true;



string::iterator IterFw = Test.begin();

string::reverse_iterator IterBw = Test.rbegin();

while( *(IterFw++) == *(IterBw++) && IterFw != Test.end() ) {

bFlag = false;

}



if( !bFlag )

cout << "it's a palindrome" << endl;

else

cout << "it's NOT a palindrome" << endl;
rhoda
2016-05-26 16:55:52 UTC
Hi Blon, There is no need of 2 for loop. and your condition is not inside loop. So, it's checking the condition only for once. Resulting unexpected result. You can try using these statement just after x=strlen(a); int flag=1; int i=0; for(int y=strlen(a)-1; y!=x/2 ;y--) { if ( a[y] != a[i++] ) flag=0; break; } If(flag) cout<<"String is palindrome"; elase cout<<"String is not palindrome";
2008-03-07 14:41:03 UTC
string Test;

cin >> Test;

bool isPalidrom = false;

char data[256];



strcpy(data, Test.c_str());

strlwr(data);

Test = data;

string::iterator IterFw = Test.begin();

string::reverse_iterator IterRev = Test.rbegin();

while(IterFw != Test.end() && isPalidrom) {

if (!isspace(*IterFw) && !ispunct(*IterFw) && !isspace(*IterRev) && !isspace(*IterRev))

{

if (*IterFw != *IterRev)

{

isPalidrom = false;

}

else

{

++IterFw;

++IterRev;

}

} // If not space or punctation

else if (isspace(*IterFw) || ispunct(*IterFw))

{

++IterFw;

}

else

{

++IterRev;

}

}



if( isPalidrom )

cout << "it's a palindrome" << endl;

else

cout << "it's NOT a palindrome" << endl;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...