I'm doing an assignment for school where we have to find three palindromes from a file containing a bunch of words.
I have the program running, but it only displays one word, and it's incorrect.
Can someone tell me what is wrong with my code?
using namespace std;
const int length = 7;
inputFile >> word;
int x = strlen(word)-1;
for(int i = 0; i <= x; i++)
if (word[i] == word[x-i])
cout << word << " ";
First of all, you need 2 loops, one to read in the word, and one to see if it is a palindrome. Right now, you are only reading in the first word.
Secondly, you need to create a boolean variable that is initiated as true and is set as false if one letter pair in the word is not equal.
Finally, you need to print out the word, and set the boolean variable back to true.