Recursive Palindrome

Feb 14, 2016 at 8:30pm
I am trying to finish my program and it keeps saying that it is a Palindrome or it is not all the time when I tweak my code. I believe the problem is in the function if else statement but I cant seem to make it right. Here is what I have.

#include <iostream>
#include<string>

using namespace std;

bool recursionPalindrome(string mString, int start, int end);

int main()
{
string Pstring;
char choice;
do
{
cout << "Type a sentence/word and I will determine if it is a Palindrome or not." << endl;
getline(cin, Pstring);
int last = Pstring.length();

if (recursionPalindrome(Pstring, 0, last))
cout << "It is a Palindrome" << endl;
else
cout << "It is not a palindrome" << endl;
cout << "\nWould you like to test another for a Palindrome? ";
cin >> choice;
cin.ignore();
} while (choice == 'Y' || choice == 'y');

cout << endl;
return 0;
}

bool recursionPalindrome(string mString, int start, int end)
{
if (mString[end] == '\0')
return true;
else
if (mString[start] == mString[end])
return recursionPalindrome(mString, start + 1, end - 1);

else return false;
}
Feb 14, 2016 at 9:39pm
int last = Pstring.length() -1;

Please use code tags in the future. It makes you code easier to read.
Feb 14, 2016 at 9:48pm
Sorry wasn't sure how to use it. I did that and it gave me a crash error.
Feb 14, 2016 at 9:55pm
All I did was change one line in your code. Line 14 below.
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
#include <iostream>
#include <string>
using namespace std;
bool recursionPalindrome(string mString, int start, int end);

int main()
{
string Pstring;
char choice;
do
{
cout << "Type a sentence/word and I will determine if it is a Palindrome or not." << endl;
getline(cin, Pstring);
int last = Pstring.length()-1;

if (recursionPalindrome(Pstring, 0, last))
cout << "It is a Palindrome" << endl;
else
cout << "It is not a palindrome" << endl;
cout << "\nWould you like to test another for a Palindrome? ";
cin >> choice;
cin.ignore();
} while (choice == 'Y' || choice == 'y');

cout << endl;
return 0;
}

bool recursionPalindrome(string mString, int start, int end)
{
std::cout << mString[start] << "  " << mString[end] << std::endl;
if (mString[end] == '\0')
return true;
else
if (mString[start] == mString[end])
return recursionPalindrome(mString, start + 1, end - 1);

else return false;
}
Last edited on Feb 14, 2016 at 9:56pm
Feb 14, 2016 at 10:04pm
It keeps saying i'm stepping out of bounds when i put that -1 in the code.
Feb 14, 2016 at 10:11pm
When I click the little gear icon at the top right of my code above, it runs fine. Is it crashing on line 32? I think you want that to be if (mString[start] == '\0'). Try that. If that doesn't fix it, what compiler are you using and what is the exact error message.
Feb 14, 2016 at 10:15pm
Not sure why it was crashing but I changed line 32 to if (start >= end) and it works now. Thank you much!

Only one it doesn't work on is " A man, a plan, a canal, Panama " something to do with white space?
Last edited on Feb 14, 2016 at 10:23pm
Feb 14, 2016 at 10:20pm
my fault, take out line 31 that I put in for testing. I think that should do it.
Feb 14, 2016 at 10:39pm
I did, I think when the spaces don't match up on both sides of the string it doesn't work.
Feb 14, 2016 at 11:01pm
the way you are testing, that sentence is not a palindrome. Yes, you are right, it is the white space, and also the commas. If you want it to test true, you'll have to remove all spaces, punctuation, and convert all the letters to one case before you pass the string to recursionPalindrome().
Last edited on Feb 14, 2016 at 11:02pm
Feb 14, 2016 at 11:04pm
Would I run a loop on the string and copy it to another? Or is there a way to remove from a string?
Feb 14, 2016 at 11:12pm
Feb 14, 2016 at 11:19pm
You could do that, or, if you're allowed to do so, here is an example of another way to remove characters from a string.

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <ctype.h>
using namespace std;


void formatString(string &str)
{
	str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
	str.erase(std::remove(str.begin(), str.end(), ','), str.end());
	for(size_t i = 0; i < str.length(); i++)
		str[i] = toupper(str[i]);
}


Just call this function on your string before you send it to recursivePalindrome();

formatString(Pstring);
Feb 15, 2016 at 2:23am
Thank you very much! It works great.
Topic archived. No new replies allowed.