Palindrome testing program crashing.

Working on a school assignment, essentialy, write a program that will receive a word (all lowercase, no spaces, terminated by a period), and determine if it is a palindrome. The program compiles fine, with no errors or warnings, but when run it crashes. I have compiled it with Microsoft Visual C++ 6.0 and Microsoft Visual C++ 2010 express and experienced the same problem.

EDIT: The exact error message that I got on my home computer is "Expression: string subscript out of range"

#include <iostream>
#include <string>
using namespace std;
void checkpal(int, string);
int main()
{ char ans;
do
{
string word1, worddelete;
int length, i;
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
cin >> word1;
length = word1.length();
for(i = 0; i <= length - 1; i++)
{
worddelete[i] = word1[i];
}
length = length - 1;
cout << endl << "Your word is " << length << " characters long.\n";
checkpal(length, worddelete);
cout << "Would you like to check another word? 'y' for yes 'n' for no: ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
return 0;
}





void checkpal(int length, string worddelete)
{
int i;
string fronthalf, backhalf;
for(i = 0; i < length / 2; i++)
{
worddelete[i] = fronthalf[i];
worddelete[length-i] = backhalf[i];
}
if(fronthalf.compare(backhalf) != 0)

cout << "This word is not a palindrome!";
else
cout << "This word is a palindrome!";
return;
}
Last edited on
worddelete, fronthald, backhalf, don't seem to ever be initialized...
string word1, worddelete;

and

string fronthalf, backhalf;


Are sufficient, no?


EDIT: And I dont think that is the problem or else the compiler would be giving an error.
Last edited on
It is because you don't initialize them (thus making them empty strings), then try to access their elements with [], iterating over length, which is not their actual length (it's 0, because the are "").
Alright, I see what you mean.

I made a mistake with

worddelete[i] = fronthalf[i];
worddelete[length-i] = backhalf[i];

It should be

fronthalf[i] = worddelete[i];
backhalf[i] = worddelete[length-i];

In order to fill the strings fronthalf and backhalf. But when i try it that way, it STILL wont run. Am I missing something else?
EDIT: I get the exact same error as previously.
EDIT2: Actualy, the prgram RUNS either way, but it crashes after I type in a word and hit enter.
Last edited on
can you post your upgraded code in code tags. use the <> button over on the right----->
When you declare worddelete or fronthalf or backhalf you don't assign a specific length to it so it is treated as null string (it has no space for a word to be assign to it)
and as a result you can't access individually each character of it because it simply doesn't have
For example:

1
2
fronthalf[i] = worddelete[i];
backhalf[i] = worddelete[length-i];


or

1
2
3
4
for(i = 0; i <= length - 1; i++)
{
worddelete[i] = word1[i];
}


Instead of that you should use the string assignment operator ( = ) to 'copy' a string to another
Try it and let me know about your progress
Last edited on
I "think" I am following you. Since I am not declaring the size of each string, I cannot access individual characters.

That I understand, but if I'm following you correctly, how do I get rid of the '.' mandated by the assignment? Since that means

1
2
3
4
for(i = 0; i <= length - 1; i++)
{
worddelete[i] = word1[i];
}


Won't work at all.



So, what I think I need is some way of determining how long the word is. Probably by searching the string for '.'? In that case should I declare the strings as some arbitrarily high number? That seems like a crude way to go though.


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
#include <iostream>
#include <string>
using namespace std;
void checkpal(int, string);
int main()
{ char ans;
do
{
string word1, worddelete;
int length, i;
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
cin >> word1;
length = word1.length();
for(i = 0; i <= length - 1; i++)
{
worddelete[i] = word1[i];
}
length = length - 1;
cout << endl << "Your word is " << length << " characters long.\n";
checkpal(length, worddelete);
cout << "Would you like to check another word? 'y' for yes 'n' for no: ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
return 0;
}





void checkpal(int length, string worddelete)
{
int i;
string fronthalf, backhalf;
for(i = 0; i < length / 2; i++)
{
fronthalf[i] = worddelete[i];
backhalf[i] = worddelete[length-i];
}
if(fronthalf.compare(backhalf) != 0)

cout << "This word is not a palindrome!";
else
cout << "This word is a palindrome!";
return;
} 
If they are std::strings, just use .size() to get the size. Also, you ought to use getline() to get data into the string instead of just cin>>, which will stop at the first space.
What is the difference between using .size() and .length()? But I see what you mean with the
getline(), I suspect that was the problem. (Though this computer does not have a compiler, so I can't check.)


And that should just be

1
2
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
getline (cin,word1);


correct?
Last edited on
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
#include <iostream>
#include <string>
using namespace std;
void checkpal(int length, char worddelete[]);
int main()

{ char ans;
do
{
string word1;
char worddelete[100];
int length, i;
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
getline(cin, word1);
length = word1.length();
cout << endl << "Your word is " << (length - 1) << " characters long.\n";
length = length - 1;
for(i = 0; i < length; i++)
{
worddelete[i] = word1[i];
}

checkpal(length, worddelete);
cout << "Would you like to check another word? 'y' for yes 'n' for no: ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
return 0;
}





void checkpal(int length, char worddelete[])
{
int i;
string fronthalf2, backhalf2;
char fronthalf[100], backhalf[100];
for(i = 0; i < length / 2; i++)
{
fronthalf[i] = worddelete[i];
backhalf[i] = worddelete[length-i];
}


if(fronthalf2.compare(backhalf2) != 0)

cout << "This word is not a palindrome!";
else
cout << "This word is a palindrome!";
} 





At the moment this is what I have.

Problem 1: Trying to figure out how to convert from type array to type string. Should figure this out on my own shortly(assuming it is possible).

Problem 2: Past the second loop it reports length as = -1. I have no idea why.

Problem 3: The program seems to have about a 50/50 chance of crashing instantly when typing in 'y' to initialize 'ans'. Not when you press enter, simply when you TYPE it. The window will just up and disappear.
Last edited on
Diffrint iteration:


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
#include <iostream>
#include <string>
using namespace std;
void checkpal(int length, char worddelete[]);
bool is_equal ( char backhalf[], char fronthalf[], int length );
int main()

{ char ans;
do
{
string word1;
char worddelete[100];
int length, i;
cout << "This program checks palindromes.\nEnter your word, terminated by a period: ";
getline(cin, word1);
length = word1.length();

length = length - 1;
cout << endl << "Your word is " << length << " characters long.\n";
for(i = 0; i < length; i++)
{
worddelete[i] = word1[i];
}

checkpal(length, worddelete);
cout << "\nWould you like to check another word? 'y' for yes 'n' for no: ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
return 0;
}


bool is_equal ( char backhalf[], char fronthalf[], int length )
{
length = length / 2;
for ( int i = 0; i < length; i++ ) {
if ( backhalf[i] != fronthalf[i] )
return false;
}

return true;
}


void checkpal(int length, char worddelete[])
{
bool a;
char fronthalf[100], backhalf[100];
for(int i = 0; i < length / 2; i++)
{
fronthalf[i] = worddelete[i];
backhalf[i] = worddelete[length];
cout << "front " << fronthalf[i] << "      back " << backhalf[i];
}

a = is_equal(backhalf, fronthalf, length);
if(a)
{
cout << "\nThis is a palindrome!";
}
else
{
cout << "\nThis is NOT a palindrome!";
}
}



For some bizarre reason the program returns false the first time and true the second time and won't run a third time. I have no idea why.
Topic archived. No new replies allowed.