Leo V.
Leo V.

Reputation: 47

How to make palindrome code not worry about the (userInput) word spacing

as described in the title, I am having trouble with my code trying to read the spaces in between the words for example "never odd or even" returns as "never odd or even is not a palindrome" but its supposed to say "never odd or even is a palindrome". Below i will provide my current code and the grading results along with the numbers i cant seem to fix.

#include <iostream>
#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<(startInput/2); i++){
if (userInput[i] != userInput[(startInput -1) -i])
isPalindrome = false; 
}
if (isPalindrome == true){
cout << userInput << " is a palindrome" << endl;
}
else {
cout << userInput << " is not a palindrome" <<endl;
}

return 0;
}

3: Input: never odd or even Your output: never odd or even is not a palindrome Expected output: never odd or even is a palindrome

5: Input: dr awkward Your output: dr awkward is not a palindrome Expected output: dr awkward is a palindrome

7: Input: no lemon no melon Your output: no lemon no melon is not a palindrome Expected output: no lemon no melon is a palindrome

Upvotes: 1

Views: 839

Answers (2)

PaulMcKenzie
PaulMcKenzie

Reputation: 35454

First, remove spaces from the string, and that can be done with a single function call in C++ by utilizing std::remove_if.

Next, compare the string that has the spaces removed with a reversed version of the string. Another one liner by creating a string using reverse iterators:

So let's break this down:

1) Removing spaces from a string:

#include <algorithm>
#include <string>
#include <cctype>
//...
std::string s;
//...
s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());

2) Build a reversed version of a string:

   std::string s;
   // ... 
   std::string sreversed == std::string(s.rbegin(), s.rend());

3) Putting this all together into a neat function:

#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>

bool isPalindrome(std::string s)
{
   // erase the spaces
   s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end());

   // compare original string with reversed string and return result
   return s == std::string(s.rbegin(), s.rend());
}

int main()
{
   std::string test = "never odd or even";
   bool result = isPalindrome(test);
   std::cout << "\"" << test << "\" is" << (result?" ":" not ") << "a palindrome";
}

Output:

"never odd or even" is a palindrome

Upvotes: 1

Matthew Coats
Matthew Coats

Reputation: 121

Why don't you remove the spaces from the string before testing if it's a palindrome? Use getline() as you are now, then remove all spaces from the input, and then do your palindrome test.

This link might be helpful for a method to remove the spaces: https://www.geeksforgeeks.org/remove-spaces-from-a-given-string/

Side note: comparing a boolean value to true like you are with if (isPalindrome == true) is not necessary. You can just use if (isPalindrome)

Upvotes: 0

Related Questions