1

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;
}
krystenr1
  • 11
  • 2
  • I suggest you try to step through the code statement by statement in a debugger, while monitoring the variables and their values. I especially think you need to to that for an input containing two consecutive spaces. And think about what [`erase`](https://en.cppreference.com/w/cpp/string/basic_string/erase) *returns*. – Some programmer dude Oct 11 '20 at 16:21
  • ah yeah, you forgot to decrease ```startInput``` if the string contains a space then it should work – ic_Engineer Oct 11 '20 at 16:21
  • It almost work fine https://godbolt.org/z/5dG591 (code is just overcomplicated). Just create string without space and you done. – Marek R Oct 11 '20 at 16:25
  • Okay so I got it to recognize that neveroddoreven is a palindrome. Thank you! But how do I have it print the original input with the spaces? – krystenr1 Oct 11 '20 at 16:47
  • create a copy, before modifying value. Or better extract a function. – Marek R Oct 11 '20 at 17:00
  • @krystenr1 I've added an answer with an alternative implementation. That implementation doesn't modify the input string. – Support Ukraine Oct 11 '20 at 17:02
  • https://godbolt.org/z/bTnoTe – Marek R Oct 11 '20 at 17:10

2 Answers2

1

After you erase all the spaces, startInput no longer refers to the actual length of the string. That means this comparison:

if (userInput[i] != userInput[(startInput -1 ) -i])

is not comparing the correct characters.

You can fix this by adding this line:

startInput = userInput.length();

after doing the erasing.

Here's a demo.


Also, in your erase code, this comparison i<userInput.length() is not a good idea, since you are comparing a signed and unsigned value. Also, you don't erase adjacent spaces. A simpler way to do that would be:

userInput.erase(std::remove(std::begin(userInput), std::end(userInput), ' '), 
                std::end(userInput));
cigien
  • 57,834
  • 11
  • 73
  • 112
0

As others have pointed out, the problem is that startInput doesn't take into account that you erase some spaces. So move the the line startInput = userInput.length(); so that it is just after the erase-loop.

An alternative solution that will not change the original input could be:

#include <iostream>
#include <string>

int main() 
{
   std::string userInput;
   std::string userInputNoSpace;  // Use one more string
   int startInput;

   bool isPalindrome = true;

   std::getline(std::cin, userInput);
   
   // Copy all characters that are NOT space to the new string
   for (auto c : userInput)
   {
      if (c != ' ')
      {
        userInputNoSpace += c;
      }
   }
   startInput = userInputNoSpace.length();
   
   for (int i = 0; i<(startInput / 2); i++)
   {
       if (userInputNoSpace[i] != userInputNoSpace[(startInput -1 ) -i])
       {
           isPalindrome = false;
           break;
       }
   }

   if (isPalindrome)
   {
      std::cout << userInput << " is a palindrome" << std::endl;
   }
   else 
   {
      std::cout << userInput << " is not a palindrome" << std::endl;
   }
   
   return 0;
}

Input:

never odd or even

Output:

never odd or even is a palindrome
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Thank you. What does auto c do here exactly? I'm unfamiliar with that. I'm only a few weeks in to learning programming from zero knowledge, so I apologize if this is a stupid question. – krystenr1 Oct 11 '20 at 17:09
  • @krystenr1 `auto c` here is the same as `char c`. It goes through the string one character at the time. Like "for each character c in the string userInput" – Support Ukraine Oct 11 '20 at 17:12