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?
Much thanks!
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
const int length = 7;
char word[length];
inputFile.open("/users/abrick/resources/american-english-insane");
int i;
inputFile >> word;
int x = strlen(word)-1;
for(int i = 0; i <= x; i++)
{
if (word[i] == word[x-i])
{
cout << word << " ";
}
}
return 0;
}
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.