-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGHP5.cpp
64 lines (61 loc) · 1.95 KB
/
GHP5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//This program reads in words from the user and checks to see if these
//words are palindromes or not
//April 2014
//Written by: Micah Church
//Language: C++ (g++ target)
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main ()
{
string w; //The string the user inputs
int i, //used as a counter in a loop
ans; //used to terminate the sentinel do while loop
char ch,ch2; //Characters used to test the word
bool palindrome; //A boolean value used to print if the word is a palindrome
int answer; //The static value ued to terminate the while loop
answer=-999; //The value that when inputted terminates the while
palindrome=1; //Initialized for use in the time saving while that
//evauluates the string
queue <char>q; //Initializes a queue of chars called q
stack <char>s; //Initializes a stack of chars called s
do
{
cout<<"Please input a word to check to see if it is a palindrome"<<endl;
cin>>w;
for(i=0;i<w.size();i++)
{
s.push(w[i]); //Putting a value onto the stack of chars
q.push(w[i]); //Putting a value onto the queue of chars
}
while(!s.empty()&&palindrome)
{
ch=s.top(); //The char on the top of the stack
ch2=q.front(); //The char at the front of the queue
if(ch==ch2)
{
s.pop(); //Deletes the first char on the stack
q.pop(); //Deletes the char at the front of the queue
}
else
palindrome=0;
}
if(palindrome)
cout<<"The word "<<w<<" is a palindrome."<<endl;
else
cout<<"The word "<<w<<" is not a palindrome."<<endl;
cout<<"Would you like to try another word? If not then input a -999."<<endl;
cin>>ans;
palindrome=1;
//Clears out the stack and queue for the next word to be able to be
//evaluated
while(!q.empty())
{
q.pop();
s.pop();
}
}
while(ans!=answer);
return 0;
}