I've searched all over to try to answer my own questions, but I'm hitting a wall here. I've been working on this same exercise for three days and getting frustrated, hence my very first post! This is a school assignment, but I really want to understand why this isn't working. When I use input "bob" it returns "bob is a palindrome" as expected. When I input "bobby" it returns "bobby is not a palindrome" as expected. All good there. It took me forever to figure out how to remove spaces from my input when using the sentence "never odd or even" but I managed to do that successfully, too. But here's the rub: (1) even after the spaces are removed, it seems to think that "neveroddoreven" is NOT a palindrome - why? What am I missing? Additionally, and this is probably a stupid question (but this is my first foray into programming and I'm a total newbie), how do I get it to output the original userInput before I removed the spaces in the final output? Currently the below code outputs "neveroddoreven is not a palindrome". Thanks in advance for any pointers you can give me.
#include <string>
#include <cctype>
using namespace std;
int main() {
string userInput;
int startInput;
bool isPalindrome = true;
getline (cin, userInput);
startInput = userInput.length();
for(int i = 0; i<userInput.length(); i++)
if(userInput[i] == ' ') userInput.erase(i,1);
for (int i = 0; i<(startInput / 2); i++){
if (userInput[i] != userInput[(startInput -1 ) -i])
isPalindrome = false;
}
if (isPalindrome){
cout << userInput << " is a palindrome" << endl;
}
else {
cout << userInput << " is not a palindrome" << endl;
}
return 0;
}