Question:
C++ word by word palindrome program?
anonymous
2007-04-14 22:03:51 UTC
I need to write a program that checks to see if a sentence is a word by word palindrome. For example, "You can cage a swallow, can't you, but, you can't swallow a cage, can you?" Is a palindrome.

I can write a program using stacks and queues to determine if the entire sentence is a palindrome(ex: Madam, I am Adam" works for my program), but I can't figure out how to do it word by word.

Can anyone help?
Three answers:
csanon
2007-04-14 22:56:08 UTC
The most logical way is to tokenize (what I mentioned in your other question). Tokenizing into a vector makes sense. Then compare the first and the last, the second and second to last, etc. Using iterators over a vector is effective here.
?
2016-05-20 06:36:24 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";
Monster
2007-04-14 22:23:45 UTC
you can use stacks. then one mainStack and one of tempStack. mainStack is contains main sentence. then use WHILE or FOR loops. like this.

int i = 0;

while (not END OF SENTENCE)

{

sentence[i] = " " then pustItToTempStack(i);

....

}

pustItToTempStack (i)

{

//this function must insert word into tempStack.

}



if word is inserted to tempStack u can pop these letters.

REmember Stack is works LIFO (last in first out)>


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