-3

We are to use cin.get() in this program, but when I use it, it can't determine whether the inputted string is a palindrome. I have even tried with cin.get(str, 0) but this reads an error message for me. This is the code I have now. Please help.

//program to for palindrome
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string str) //will return a true or false value
{
   int leng = str.length(); //local variable for length
   char ch1 = str[0]; //cases if string is 0
   char ch2 = str[leng - 1];//cases if string is not 0
   if(ch1 != ch2) //base case
   {
       return false;
   }
   else
   {
       if(str.length() <= 1)
       {
           return true;
       }
       return isPalindrome(str.substr(1, leng - 2)); //recusive computation
   }
}

int main()
{
   string str; //declare variable str
   cout << "Enter a line that might be a palindrome: "<< endl;
   cin.get();
   bool pal = isPalindrome(str); //assign pal 
   if(pal)
   {
       cout <<"The string is a palindrome." << endl;
   }
   else
   {
       cout <<"The string is NOT a palindrome." << endl;
   }
   return 0;
}

The issue is, when it does work, it only checks for palindrome on the word in the sentence, instead of the whole sentence. I know the solution for this will be ignoring or removing the space and the non-alpha letters, but I still get errors doing that.

this is an output sample:

csh> pal
      Enter a line that might be a palindrome:
      Go hang a salami, I'm a lasagna hog.
      The string is a palindrome.

csh> pal
      Enter a line that might be a palindrome:
      This is another candidate string.
      The string is NOT a palindrome.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
asang003
  • 3
  • 1
  • 1
    When you are asking a question on Stack Overflow, and you claim the code in question produces an error message, please provide the _exact text_ of the error message in the body of your question. – Wyck Sep 09 '21 at 03:59
  • 1
    For code that compiles but does not function properly, you should provide both the expected and actual output. Which did you provide? (It does look like the expected output, but you explicitly state which it is.) What debugging have you done to narrow down the problem? Typically, a good SO question focuses on a single error to the point where the question does not need to mention the original assignment, except maybe as a footnote for context. The code gets abstracted into a [mre] that is designed to demonstrate the error rather than fulfill the assignment. – JaMiT Sep 09 '21 at 04:20
  • Apart from your user-input mistake, the rest of your question is already answered in other questions. e.g.: [how to strip non alphanumeric characters from the string](https://stackoverflow.com/questions/6319872/how-to-strip-all-non-alphanumeric-characters-from-a-string-in-c) and [How to check if a string is a palindrome](https://stackoverflow.com/questions/8362572/check-if-a-string-is-palindrome). – Wyck Sep 09 '21 at 04:23
  • Sorry, a word got lost in my earlier comment. I meant to write "you **should** explicitly state which it is". – JaMiT Sep 09 '21 at 21:22

1 Answers1

1

Nothing in your code assigns str a value. You are passing an empty string to isPalindrome(). And so you are accessing str[leng - 1] when str is empty and leng is 0, which is likely the error you are encountering.

Consider using the following code to read the entire line of the user's input, spaces and all (up to just before they press the Enter key) into the str variable.

getline(cin, str);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • The OP said "*We are to use `cin.get()` in this program*" - so `std::getline()` is likely not an option. But `get()` has an overload that can read a whole line of text into a `char[]` array, which can then be assigned to a `std::string`. Or use the overload that reads into a `std::streambuf`, using the buffer of a `std::ostringstream`, and then extract a `std::string` from that. – Remy Lebeau Sep 09 '21 at 04:41