Problem description A palindrome1 is a string that reads the same forward and ba
ID: 3716447 • Letter: P
Question
Problem description
A palindrome1 is a string that reads the same forward and backward; that is, the letters are the same whether you read them from right to left or from left to right. For example, the one-word string "radar" is a palindrome. A more complicated example of a palindrome is the following sentence:
Able was I ere I saw Elba.
Palindromes are fun to make up, and they even have applications in at least one area -- the analysis of genetic material.
Suppose we want a program to read a line of text and tell us if the line is a palindrome. We can do this by using both a stack and a queue. We will read the line of text into both a stack and a queue, and then write out the contents of the stack and the contents of the queue. The line that is written using the queue is written forward, and the line that is written using the stack is written backward. Now, if those two output lines are the same, then the input string must be a palindrome. Of course, the program need not actually write out the contents of the stack and the queue. The program can simply compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.
When checking for a palindrome, we treat both the upper- and lowercase versions of a letter as being the same character. This is because we want to consider a sentence as reading the same forward and backward even though it might start with an uppercase letter and end with a lowercase letter. For example, the string "Able was I ere I saw Elba" when written backwards reads "ablE was I ere I saw elbA". The two strings match, provided we agree to consider upper- and lowercase versions of a letter as being equal.
We also want to ignore spaces and punctuation, requiring only that the letters on the line read the same forward and backward. This way we can find more palindromes. If we look only at letters, discarding both spaces and punctuation, then there are many more palindromes. However, they are not always easy to spot. For example, you might not immediately recognize the following as a palindrome:
Straw? No, too stupid a fad. I put soot on warts.
Another kind of palindrome is a string of words such that the words read the same forward and backward. For example, the phrase:
You can cage a swallow, can't you, but you can't swallow a cage, can you?
is a word-by-word palindrome.
1 https://en.wikipedia.org/wiki/Semordnilap
4/19/18 8:15 PM 2
Specification
Write a C++ program using the STL to test an input string and tell whether or not it is a word-by-word palindrome. Consider upper- and lowercase letters to be the same letter. Define a word as any string consisting of only letters or an apostrophe and bounded at each end with one of the following: a space, a punctuation mark, the beginning of the line, or the end of the line. Your program should have a friendly interface and allow the user to check more lines until the user says he or she wishes to quit the program.
?name the file palindrome.cpp ?
Here are a couple of test inputs:
DUSK TO DAWN (Vice Versa)
Rays of sun now burn and shine. Haze of red, horizon's line, Gleams and glows now far away, Beams with dreams dissolving day. So it comes and this is born- Glow with dusk reversing dawn. Light of hours now goes to dark, Night removing daylight's spark.
Spark lights day, removing night. Dark too goes, now hours of light Dawn, reversing dusk with glow. Born is this and comes it so. Day, dissolving dreams with beams, Way afar now glows and gleams. * Line horizons, red of haze! Shine and burn now, sun of rays!
©MARK SCRIVENER 2006
Love is This and This is Love by J.A. Linden
Darling, my love Is great, so great; Recalling Heaven's calm above. Fate is sweet this--- All after Fall! Fall? After all, This, sweet, is fate-- Above calm Heaven's recalling.
Great, so great is Love, my darling!
Explanation / Answer
/*The following is your cpp code. Just copy paste it and save it as palindrome.cpp and execute it.
For every input string is taken untill enter is pressed. Now ever word is converted to upper case.
After that from every word only character is is kept and punctuation mark is removed.
For palindrome checking simply one queue q and one stack s is taken.
*/
#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
int main(){
queue<string> q;
stack<string> s;
string word;
char ch;
do{
int m=0;cin.clear();
cout << "Enter a line and I will see if it's a palindrome:" << endl;
//string will be taken untill enter(' ') is pressed.
while (cin.peek( ) != ' ')
{
cin >> word;
transform(word.begin(), word.end(), word.begin(), ::toupper);
//cout<<word<<endl;
string st="";
int i=0;
/*taking only A to Z character from every word as we are supposd to ignore other punctuation character.*/
while(word[i]>='A'&&word[i]<='Z'){st+=word[i];i++;}
//cout<<st<<endl;
q.push(st);
s.push(st);
}
/*After processing the sentence from backword(in stack s) and from beginning(from queue q) word
is taken and compared to check mismatches(m).*/
while ((!q.empty( )) && (!s.empty( )))
{
if (q.front( ) != s.top( ))
++m;
q.pop( );
s.pop( );
}
/*finally if there is 0 mismatch, given string is palindrome else not palindrome.*/
if (m == 0)
cout << "That is a palindrome." << endl;
else
cout << "That is not a palindrome." << endl;
cin.clear();
/*To continue untill user want he/she can keep on pressing c.*/
char c;cout<<"press c to continue and any other character to quit"<<endl;cin>>c;ch=c;
//Emptying cin buffer.
cin.ignore();
}while(ch=='c'||ch=='C');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.