palindrome

This program takes a string as an input, and checks if is a palindrome.
This is supposed to be case and space insensitive.

Once I run it, the command prompt pops up, but as soon as I press enter
after entering a space, or entering 'n' after the message (try again(Y/N)?),
the program crushes, a new window called 'xutility' appears and
it indicates line 221 :
*_Pnext != nullptr; *_Pnext = (*_Pnext)->_Mynextiter)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
#include<string>
#include<algorithm>
#include<cctype>

using namespace std;

void palindrome(string a)
{
	transform(a.begin(), a.end(), a.begin(),
		[](unsigned char c) { return std::tolower(c); });

	remove_if(a.begin(), a.end(), isspace);

	if (a == string(a.rbegin(), a.rend()))
	{
		cout << "this is a palindrome" << endl;
	}
	else
	{
		cout << "not a palindrome" << endl;
	}
}

int main(string b)
{
	char answer;
	do
	{
		cout << "enter a string" << endl;
		cin >> b;
		
		palindrome(b);

		cout << "try again(Y/N)?" << endl;
		cin >> answer;
	} while (answer == 'Y' || answer == 'y');
		return 0;

}
int main(string b)

No no no. This is an illegal form of main. This should read: int main()
Thank you.
The issue is solved, but now there is another one.
The program, after showing the "try again" message,
it stops and does not let me choose yes or no.
Post your exact code, because what we see now won't compile (with or without 'string b')... at least on gcc.

Edit:

Your code (w/ string b; on its on line) will probably compile with Visual Studio, but not with GCC.
https://stackoverflow.com/questions/21578544/stdremove-if-and-stdisspace-compile-time-error
A possible fix to make it compile on both compilers is: https://stackoverflow.com/a/21578623/8690169
The issue is that if the <locale> header is internally included by the implementation, "isspace" is ambiguous because there's another overload for it.
https://en.cppreference.com/w/cpp/locale/isspace

Regardless, show your latest code.
Last edited on
The proper way to do it is like so:

1
2
3
    str.erase(std::remove_if(str.begin(), str.end(),
                [](unsigned char c){ return std::isspace(c); }),
            str.end());

And if he wants to allow spaces in the string (which he obviously does) then he needs to use getline.
Probably the most common palindrome is "Madam, I'm Adam"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
   char answer;

   do
   {
      // get a whole line of text, including spaces and punctuation
      std::cout << "Enter a string:\n";
      std::string input;
      std::getline(std::cin, input);
      std::cout << '\n';

      // create a 'temp' string to modify for palindrome testing
      std::string pali = input;

      // erase whitespace
      pali.erase(std::remove_if(pali.begin(), pali.end(), ::isspace), pali.end());

      // erase punctuation
      pali.erase(std::remove_if(pali.begin(), pali.end(), ::ispunct), pali.end());

      // convert the string to lower case
      // could use upper case if desired
      std::transform(pali.begin(), pali.end(), pali.begin(), ::tolower);

      // compare to a temp reversed string
      if (pali == std::string(pali.rbegin(), pali.rend()))
      {
         std::cout << '\"' << input << "\" is a palindrome.\n";
      }
      else
      {
         std::cout << '\"' << input << "\" is NOT a palindrome.\n";
      }

      std::cout << "\nDo you want to try again? ";
      std::getline(std::cin, input);
      answer = input[0];
   } while ('y' == ::tolower(answer));
}
Enter a string:
Madam I'm Adam

"Madam I'm Adam" is a palindrome.

Do you want to try again? y
Enter a string:
Hello George

"Hello George" is NOT a palindrome.

Do you want to try again? n
Last edited on
Ganado wrote:
Your code (w/ string b; on its on line) will probably compile with Visual Studio

It does (VS 2019), but it is still illegal according to the standard.

Go figure. I didn't think it would until I tested it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip> // std::quoted
// #include <algorithm> // for std::equal

// remove non alphanumeric; convert to all lower case
std::string clean( const std::string& str )
{
    std::string cleaned ;

    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    // note: unsigned char https://en.cppreference.com/w/cpp/string/byte/isalnum#Notes
    for( unsigned char c : str )
        if( std::isalnum(c) ) cleaned += std::tolower(c) ;

    return cleaned ;
}

bool palindrome( std::string str )
{
    str = clean(str) ;
    return str == std::string( std::rbegin(str), std::rend(str) ) ;

    // or: https://en.cppreference.com/w/cpp/algorithm/equal#Example
    // return std::equal( str.begin(), str.begin() + str.size()/2, str.rbegin() ) ;
}

bool again()
{
    char answer ;
    std::cout << "again? " ;
    std::cin >> answer ;
    return answer == 'Y' || answer == 'y' ;
}

int main()
{
    do
    {
        std::cout << "enter a string\n" ;
        std::string str ;
        while( str.empty() ) std::getline( std::cin, str ) ; // skip empty lines
        // note: this skips the new line left in the buffer after input to again()

        std::cout << "the string " << std::quoted(str) << " is " ;
        if( !palindrome(str) ) std::cout << "not " ;
        std::cout << "a palindrome\n" ;
    }
    while( again() ) ;
}
Interesting way to look at the problem, JLBorges. I'm gonna have to take a few bites and ruminate on how you do things for stripping out the extraneous characters.
thank you everybody for your feedback.
However, would it be possible to slightly edit my code in order to make it work?
Let me know if I am a way off the track.
And by the way, I am using visual studio 2017.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include<string>
#include<algorithm>
#include<cctype>

using namespace std;

void palindrome(string a)
{
	transform(a.begin(), a.end(), a.begin(),
		[](unsigned char c) { return std::tolower(c); });

	//remove_if(a.begin(), a.end(), isspace);

	if (a == string(a.rbegin(), a.rend()))
	{
		cout << "this is a palindrome" << endl;
	}
	else
	{
		cout << "not a palindrome" << endl;
	}
}

int main()
{
	char answer;
	do
	{
		string b;
		cout << "enter a string" << endl;
		cin >> b;
		
		palindrome(b);

		cout << "try again(Y/N)?" << endl;
		cin >> answer;
	} while (answer == 'Y' || answer == 'y');
		return 0;

}
If you're going to type in lines containing spaces, you can't use cin >>, because cin >> will stop at the first space. Then, the NEXT cin >> will read the NEXT word.

That's why it ends if you enter a line with a space. Because cin >> answer; reads that next word, and that next word isn't 'Y' so the program ends.

Try getline.
Last edited on
by keeping the code the way it is and changing line 37 to "getline", an error shows up and the word "getline" in line 37 gets underlined in red.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include<string>
#include<algorithm>
#include<cctype>

using namespace std;

void palindrome(string a)
{
	a.erase(std::remove_if(a.begin(), a.end(),
		[](unsigned char c) { return std::isspace(c); }),
		a.end());

	//remove_if(a.begin(), a.end(), isspace);

	if (a == string(a.rbegin(), a.rend()))
	{
		cout << "this is a palindrome" << endl;
	}
	else
	{
		cout << "not a palindrome" << endl;
	}
}

int main()
{
	char answer;
	do
	{
		string b;
		cout << "enter a string" << endl;
		getline(cin, b);
		
		palindrome(b);

		cout << "try again(Y/N)?" << endl;
		getline(cin, answer);
	} while (answer == 'Y' || answer == 'y');
		return 0;

}
"underlined in red" is not a helpful problem description. What compiler error are you getting?
getline() in this form requires a string; variable answer is only a char.

Either make answer a string (in which case you would need double quotes for both possibilities on line 39) or clear the remainder of the input up to and including the line feed by changing line 38 to:
cin >> answer; cin.ignore( 1000, '\n' );

Your earlier getline() on line 33 is entirely correct.
Last edited on
Topic archived. No new replies allowed.