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.
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
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.
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:
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
bool is_letter(char c)
{
string s;
char first = s[0];
returnfalse;
}
bool to_lower(char c)
{
if(c >= 'A' && c <= 'Z')
return c + 32;
return c;
}
bool is_palindrome(string s)
{
if (s.length() <= 1)
returntrue;
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);
}
returnfalse;
}
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.
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.
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!
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.
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);
}