What I am trying to accomplish here is determining whether a word is a palindrome using queues. My program compiles and executes fine. The only problem is that I am getting the wrong output and need some assistance. I put a bool variable isPalin, but I do not know if I am using it correctly.
Current output:
racecar is a Palindrome
123456789 is NOT a Palindrome
mom is NOT a Palindrome
dog is NOT a Palindrome
noon is NOT a Palindrome
Expected output:
racecar is a Palindrome
123456789 is NOT a Palindrome
mom is a Palindrome
dog is NOT a Palindrome
noon is a Palindrome
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "queue.h"
usingnamespace std;
int main()
{
stringstream ss;
string line; // name of my identifier used to read in data
string word; // individual characters
queueType<string> u; // declares queue u
queueType<string> r; // declare queue r
queueType<string> s; // declare queue s
int counter = 0; // declares counter
ifstream inData("input.txt"); // declaration of inData
ofstream outData("output.txt"); // declaration of outData
u.initializeQueue();
r.initializeQueue();
s.initializeQueue();
while ( getline( inData, line ) ) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/
ss << line;
while ( ss >> word )/*there are still characters*/
{
u.addQueue(word);
s.addQueue(word); /*adds characters of word into queues u and s*/
}
while (!u.isEmptyQueue()) /*u is not empty*/
{
r.addQueue(u.front());
u.deleteQueue();
/*adds to the front of u into r and then delete from u*/
}
bool isPalin=true;
while (!s.isEmptyQueue())/*s is not empty*/
{
s.deleteQueue();
u.addQueue(word);
if(!(u.front() == r.front())) /*compares front elements, does something if the two not equal, delete elements*/
{
u.deleteQueue();
r.deleteQueue();
isPalin=false;
break;
}
}
if (isPalin)
{
outData<<word<<" is a Palindrome"<<endl;
}
else
outData<<word<<" is NOT a Palindrome"<<endl;
ss.clear();
}
inData.close(); // closes inData
outData.close(); // closes outData
system("PAUSE");
return 0;
}
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "queue.h"
usingnamespace std;
int main()
{
stringstream ss;
string line; // name of my identifier used to read in data
string word; // individual characters
queueType<string> u; // declares queue u
queueType<string> r; // declare queue r
queueType<string> s; // declare queue s
int counter = 0; // declares counter
ifstream inData("input.txt"); // declaration of inData
ofstream outData("output.txt"); // declaration of outData
while ( getline( inData, line ) ) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/
ss << line;
while ( ss >> word )/*there are still characters*/
{
u.addQueue(word);
s.addQueue(word); /*adds characters of word into queues u and s*/
}
while (!u.isEmptyQueue()) /*u is not empty*/
{
r.addQueue(u.front());
u.deleteQueue();
/*adds to the front of u into r and then delete from u*/
}
bool isPalin = true;
while (!s.isEmptyQueue())/*s is not empty*/
{
s.deleteQueue();
u.addQueue(word);
if(!(u.front() == r.front())) /*compares front elements, does something if the two not equal, delete elements*/
{
isPalin = false;
break;
}
else
{
u.deleteQueue();
r.deleteQueue();
}
}
if (isPalin == true)
{
outData<<word<<" is a Palindrome"<<endl;
}
if (isPalin == false)
outData<<word<<" is NOT a Palindrome"<<endl;
ss.clear();
}
inData.close(); // closes inData
outData.close(); // closes outData
system("PAUSE");
return 0;
}