Palindrome

I have this program that would run a Palindrome, but it includes blank spaces, punctuation, and is case-sensitive. And i need to ignore the blank spaces, punctuation and case-sensitive. the hint given to figure it out is

bool is_letter(char c);
and
char to_lower(char c);

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
  #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;
}

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


Please and thank you.
Last edited on
string s = ...
string plain;
copy lowercased characters from s to plain if they are letters
test if plain is a palindrome
I would start my making a second string that consists of just the letters in s, converted to lower case. So if s is "I HATE my class!!" then the new string would be "ihatemyclass".

Once you have that string you can check for a palindrome easily.

Also, the algorithm that you're using is inefficient. You're creating a new string and doing a recursive call each time, so if palindrome is 100 characters then you create 50 new strings. You could do it easier by comparing characters at the front and back of the string and working your way in towards the middle.
you would need to create a function which takes the string of the palindrome and puts it either all upper case or all lower case

in my example, i made it all upper case

1
2
3
4
for (int i = 0; word[i] != '\0'; i++) //as long as word does not equal null
	{
		word[i] = toupper(word[i]); //initialize each word in sentence to uppercase
	}


where word is the word/sentence the user input
Topic archived. No new replies allowed.