checking for palindromes

closed account (9Nh5Djzh)
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.
Last edited on
closed account (9Nh5Djzh)
thank you!
If this is reading just the first word, how do I read all of the words?
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
#include <iostream>
#include <fstream>

int main()
{
	std::ifstream inputFile("/users/abrick/resources/american-english-insane");
	if(!inputFile)
	{
		return 0;
	}
	std::string word;
	int counter;
	bool palincheck=true;
	
	while(inputFile)
	{
		inputFile>>word;
		for(counter=0;counter<word.length()/2;counter++)
		{
			if(word[counter]!=word[word.length()-counter-1])
			{
				palincheck=false;
				break;
			}
		}
		if(palincheck==true)
		{
			std::cout<<word<<std::endl;
		}
		palincheck=true;
	}
return 0;
}
are you doing this for mike wright cs2450 at uvu? i'm in your class
closed account (9Nh5Djzh)
Wow, thanks usandfriends!!!!
Contario, I'm actually in San Francisco, I guess a lot of teacher assign the same sort of work :)
Topic archived. No new replies allowed.