Im trying to test whether or not the lines in a file are a p
I\'m trying to test whether or not the lines in a file are a palindrome or not but I can\'t seem to get my code to work. Please let me know if you see any errors or anything I can change. I put the output that I\'ve been getting on the bottom.
#include <fstream> // for file I/O
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include \"Stack.h\"
#include \"Queue.h\"
using namespace std;
int main() {
Queue Q;
Stack S;
ifstream fin;
fin.open(\"palindrome.txt\");
if (fin.fail()) {
cout << \"Input file opening failed.\ \";
exit(-1);
}
string line;
while (getline(fin, line))
{
int mismatches = 0;
for (int i =0; line[i] != \'\\0\'; i++)
{
for(int i=0; i<line.length(); i++)
{
if(line[i] == \' \') line.erase(i,1);
if(ispunct(line[i])) line.erase(i,1);
line[i] = tolower (line[i]);
}
}
S.push(line);
Q.enqueue(line);
if(Q.get_front()!=S.get_top())
{
++mismatches;
Q.dequeue();
S.push(line);
}
if (mismatches == 0)
cout << line << \" (palindrome)\" << endl;
else
cout << line << \" (not a palindrome)\" << endl;
}
fin.close();
return 0;
}
This is the output I\'ve been getting from this (i know this is wrong so please tell me how to fix my code so that it prints correctly, thank you!)
amanaplanacanalpanama (palindrome)
dogeeseseegod (not a palindrome)
neveroddeven (not a palindrome)
neveroddoreven (not a palindrome)
eye (not a palindrome)
ear (not a palindrome)
ablewasiereisawelba (not a palindrome)
wasiteliotstoiletisaw (not a palindrome)
racecars (not a palindrome)
Solution
length = strlen(string);
for(s=0; s < length; s++){
if(string[s] != string[length-s-1]){
flag = 1;
break;
}
}
You can write this function in your program to find whether a line is palindrome or not.

