Palindrome: string subscript out of range

Hi Guys,
I am trying to create a simple palindrome program.
I get the error string subscript out of range.
I am learning c++ on my own and have spent many hours trying to figure out what is wrong. Help is most appreciated. Thanks!

I believe the error is either in the while or for loops.

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
  
#include <cctype>
#include <string>
#include <iostream>
using namespace std;

//cleans string, converts to lower
bool cleanTolower(const string& s);

int main()
{
	string input;
	cout << "Hello, enter words into the palindrome checker: \n";
	getline(cin, input);

	if (cleanTolower(input))
		cout << input + " " + "is a palindrome \n ";
	else
		cout << input + "is not a palindrome \n";

	return 0;

}

bool cleanTolower(const string& s)
{
	string temp;
	string cleanString;

	string::size_type size = s.size();

	temp = s;

	string::size_type j = 0;
	string::size_type i;
	

	for (i = 0; i < size; i++)
	{
		
		if (isalpha(temp[i])){
			cleanString[j] = (tolower(temp[i]));
			j++;
			
		}
	}


	string::size_type start = 0;
	string::size_type end = cleanString.size();
	bool isPal;
	

	while (start < end)
	{
		if (cleanString[start] == cleanString[end])
		{
			isPal = true;
			start++, end--;
		}
		else
			isPal = false;
	}
	
	return isPal;

}

Last edited on
What output causes the error ?
For all the outputs i gave , i just got <input> is not a palindrome.
If I may suggest a little modification to the funtion cleanTolower()
1
2
3
4
5
6
7
bool isPalindrome(const string& s) //changed the name , cleanTolower is misleading
{
	for(std::size_t i = 0 , j = s.size() - 1; i < j ; ++i,--j)
        if(tolower(s[i]) != tolower(s[j]))
            return false;//if the corresponding characters are not same quit early.
    return true;//if we made it through the loop it's a palindrome.
}

The above code is much smaller (and easier to read .)
Further if you want to check if a line containing white-spaces is palindrome(spaces , tabs ..) , you can first the copy the string s , and remove all the spaces from it.Like this ,
1
2
3
4
5
6
7
8
	std::string copy = s;
	for(auto it = copy.begin(); it != copy.end() ;)
	{
        if(iswspace(*it))
            it = copy.erase(it);
        else
            ++it;
	}

you can add this code to the isPalindrome() function above (and replace 's' with 'copy' in the loop).

Hope that helps.
In cleanToLower(), you have:

50
51
52
53
54
55
56
57
58
59
60
61
62
63
	string::size_type end = cleanString.size();
	bool isPal;
	

	while (start < end)
	{
		if (cleanString[start] == cleanString[end])
		{
			isPal = true;
			start++, end--;
		}
		else
			isPal = false;
	}


Remember that in C++, container indices start at 0, not 1. So in the first iteration of that while loop, cleanString[end] is past the end of the string.

Last edited on
Topic archived. No new replies allowed.