palindrome program

so this program is suppose to read from a file and see if its a palindrome or not, there is something off with my code and i was wondering if anyone can see what it is exactly...

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

bool isPalindrome(char []);

const int MAX_CHARS = 81;

int main()
{
	ifstream inFile("data1.txt");
	char fileLine[MAX_CHARS];
	char formatLine[MAX_CHARS];
	bool palindrome = false;
	int newIndex = 0;

	if(!inFile)
	{
		cout << "Error" << endl;
		exit(1);
	}

	inFile.getline(fileLine, MAX_CHARS);

	while(inFile)
	{
		for(int i = 0; i < static_cast<int>(strlen(fileLine)); i++)
		{
			if(isalpha(fileLine[i]))
			{
				formatLine[newIndex] = fileLine[i];
				newIndex++;
			}
		}
		formatLine[newIndex + 1] = NULL;
		palindrome = isPalindrome(formatLine);

		cout << fileLine << endl;
		if(palindrome)
		{
			cout << "This phrase is a palindrome." << endl;
		}
		else
			cout << "This phrase is not a palindrome." << endl;

		newIndex = 0;
		inFile.getline(fileLine, MAX_CHARS);
	}

	inFile.close();

	return 0;
}

bool isPalindrome(char phrase[])
{
	bool palindrome = true;

	for(int i = 0, x = (strlen(phrase)-1); i <= x; i++, x--)
	{
		if(tolower(phrase[i]) != tolower(phrase[x]))
		{
			palindrome = false;
			return palindrome;
		}
	}

	return palindrome;
}


its suppose to ignore spaces, punctuation, and uppercase/lowercase, if you want to make an example data1.txt here's some text:

Radar
Mom
Race car
Dennis sinned
Murder for a jar of red rum
Are we not drawn onward we few drawn onward to new era
not a Palindrome.

Thanks
Last edited on
also i didnt comment much of anything so I'll just say that im reading each line into an c string and then formatting the line (no spaces or punctuation) into another c string and then testing that c-string
Your 'copy only letters' algorithm is wrong
i tried:
strcat(formatLine + newIndex, fileLine + i)
as well and that didnt work either
formatLine[newIndex + 1] = NULL; that index is incorrect.
oh wow, thanks it works now
Topic archived. No new replies allowed.