Palindrome Problems

I've run into a small issue with my palindrome code that I need to do for homework and I was hoping somebody could help me. I need the program to ignore the blank spaces, the punctuation, and it shouldn't be case sensitive. This is my code currently but I'm confused on how to put together the code for ignoring them.

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool is_palindrome(string s)
{
	if (s.length() <= 1)
		return true;

	char first = s[0];
	char last = s[s.length()-1];

	if (first == last) {
		string shorter = s.substr(1, s.length()-2);
		return is_palindrome(shorter);
	}
	return false;
}


bool is_letter (char c)
{
		
	char first = s[0];	
	if (!is_letter (first))
		return is_palindrome(s.substr(1));

}



char to_lower(char c)
{
	if (c >= 'A' and c <= 'Z')
		return c + 32;
	return c;
}


main()
{
	cout << "Enter a string: ";
	string input;
	getline(cin, input);
	cout << "\'" << input << "\' is ";
	if (!is_palindrome(input))
		cout << "not ";
	cout << "a palindrome. \n";
}
Last edited on
A couple things.
1) In line 36 you use the word "and"if (c >= 'A' and c <= 'Z'). Instead use the && operator like such if (c >= 'A' && c <= 'Z')
2) Check the is_letter function. Is it really needed here?
3) There actually already is a tolowerfunction included in the cctype header. It only however takes a character. Though it should be simple to define one using a loop. I won't do it here to give you a chance to figure it out on your own though if you end up really needing it, I can post it.
4)You can define a function that would create a copy of the string without spaces, commas, etc. To give you a starting point perhaps try using something along the lines of
1
2
3
4
for(int i = 0; i <= string.length() - 1;i++){
     if(string[i] != ' ' || string[i] != ',')
          lonelystring += string[i];
}

Note this wouldn't actually work if you use the above lines of code. It is just to give you a starting point.
5)After all of these simply call the reverse function on lonelystring and see if it is equal to lonelystring.


Hope I was a help. If you need me to post some actual working code for you instead of just the framework for it, just ask.
Too Explosive wrote:
1) In line 36 you use the word "and"if (c >= 'A' and c <= 'Z'). Instead use the && operator like such if (c >= 'A' && c <= 'Z')


and is a keyword in C++, and has exactly the same meaning as &&. There are a bunch or other ones: or not xor etc. http://en.cppreference.com/w/cpp/language/operator_alternative
Thank you Too Explosive for this help! It was greatly appreciated and thank you for the link TheIdeasMan. I took the weekend to work on the code and I've been in and out of tutoring offices for my computer science courses but the difficulty is still an issue. Currently my code is this:

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool is_letter(char c)
{
	string s;
	char first = s[0];
	return false;
}

bool to_lower(char c)

{
	if(c >= 'A' && c <= 'Z')
		return c + 32;
	return c;
}

bool is_palindrome(string s)
{
	if (s.length() <= 1)
		return true;

	char first = s[0];
	char last = s[s.length()-1];

	if (!is_letter(first))

		return is_palindrome(s.substr(1));

	if (first == last)
	{
		string shorter = s.substr(1, s.length()-2);
		return is_palindrome(shorter);
	}
	return false;
}

main()
{
	cout << "Enter a string: ";
	string input;
	getline(cin, input);
	cout << "\'" << input << "\' is ";
	if (!is_palindrome(input))
		cout << "not ";
	cout << "a palindrome. \n";
}


I believe I was able to have the function ignore the spaces and the capitalization but the issue I am actually facing now is that the program is saying everything is a palindrome. So, I'm unsure if I fixed my issue of spacing and capitalization while missing the ability for it to say some words are not palindromes or if I just wrote a program that says everything is a palindrome.
(1) You need to call a "clean" function from main that takes the string and cleans it out from all punctuation or spaces or number-characters. You can do this using the erase() function in the string class. Loop through the string, and if string[i] is punctuation, number or a space, then erase that character. Return this modified string back to main.

(2) Pass this modified string (now with no punctuation or spaces or number-characters) to a new function which makes every character lowercase. The function will do this by looping through the string and using the c++ built in tolower() function on each individual character. Return this modified string to main. Remember to use the tolower function already provided by c++. There is no need for the to_lower() function that you made. You can delete that.

(3) Finally you can pass this string (now all lower case, no punctuation, spaces or numbers) to the is_palindrome function that you had in the very first post of this thread.

So in total, main should call three functons: clean(string), tolower(string), is_palindrome(string).

Dont call functions from any other function other than main.

Hope this helps.
Last edited on
Arslan7041 wrote:
Dont call functions from any other function other than main.


I am guessing you meant for this particular task, not in general. The OP is using recursion. Your other advice is good though :+)

@therpgking

Let's do some rubber duck debugging :+) That is explain how something works to your rubber duck toy, in the process realise what is wrong. Sounds mad but it works :+D So how does your is_letter function work, what does it do?

Could do the same for the is_palindrome function, what happens with line 32?

Edit: main must return an int. That's in the standard. Google it if you wonder why.
Last edited on
I am guessing you meant for this particular task, not in general.


Actually, I meant in general, but aside from recursion. I've always learned that, whenever possible, avoid calling a different function from a function other than main.

Also, I recently learned about the rubber ducky thing with programmers. I LOL'd when I heard about that!
Last edited on
Actually, I meant in general, but aside from recursion. I've always learned that, whenever possible, avoid calling a different function from a function other than main.


I think that might be OK for something small, but is a bit extreme for anything of reasonable size. Often larger applications have a main which only calls the Application::Execute() function and nothing else. And what of a class with private helper functions which are called by other member functions?

I am struggling to see how that concept would work, even for a small program with only 1000 LOC, never mind applications with 1 MLOC.
@TheIdeasMan: I guess I have this understanding because I am not so much advanced in c++ yet. Still learning :)
I think you'll find it much easier to create a function that will take the input string, strip out the non-letters and convert it to upper (or lower) case. Then check this scrubbed string to see if it's a palindrome using your original code. So it would look something like this:

1
2
3
4
5
6
7
8
9
10
11
// Return the input string with non-letters removed, and letters converted to lower case.
string scrub(const string &str);

// Recursively determine if str is a palindrome.
// WARNING: str must be the output of scrub(0)!
static is_palindrome_scrubbed(const string &str);

is_palindrome(cons string &s) {
    string scrubbed = scrub(s);
    return is_palindrome_scrubbed(scrubbed);
}
Topic archived. No new replies allowed.