palindrome print backwards

This program should check if the input is a (case and space insensitive) palindrome.
If not, the reversed input should be printed.
It works as it should, the only thing is that I would like to see the
reversed print with spaces included, as the spaces are automatically trimmed
due to line 10.

Is that a way to do so?


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
  #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); }), // space insensitive
		a.end());

	transform(a.begin(), a.end(), a.begin(), ::tolower); //case insensitive

	if (a == string(a.rbegin(), a.rend()))
	{
		cout << "this is a palindrome" << endl;
	}
	else
	{
		cout << "not a palindrome" << endl;
		
		reverse(begin(a), end(a));
		cout << a << '\n';
	}
}

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

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

Last edited on
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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>

// remove spaces, make all lower case
std::string clean( const std::string& str )
{
    std::string result ;
    for( unsigned char c : str ) if( !std::isspace(c) ) result += std::tolower(c) ;
    return result ;
}

void print_pal_result( const std::string& str )
{
    const auto cleaned = clean(str) ;

    if( cleaned == std::string{ cleaned.rbegin(), cleaned.rend() } )
        std::cout << "string " << std::quoted(str) << " is a palindrome\n" ;

    else
    {
        std::cout << "    string " << std::quoted(str) << " is not a palindrome\n"
                  << "reverse is " << std::quoted( std::string{ str.rbegin(), str.rend() } ) << '\n' ;
    }
}

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

int main()
{
    do
    {
        std::cout << "enter a string: " ;

        std::string str ;
        while( str.empty() ) // skip empty input lines
            std::getline( std::cin, str ) ;

        print_pal_result(str) ;
    }
    while( again() ) ;
}
Last edited on
Would also suggest using std::string_view instead of simply std::string or const std::string&.
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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool isPalindrome( const string &s )
{
    int p = 0;   int q = s.size() - 1;
    while( true )
    {
        while( p < s.size() && !isalnum( s[p] ) ) p++;
        while( q >= 0       && !isalnum( s[q] ) ) q--;
        if ( p >= q ) return true;
        if ( tolower( s[p++] ) != tolower( s[q--] ) ) return false;
    }
}


int main()
{
   char ans = 'y';
   while ( tolower( ans ) == 'y' )
   {
      string test;
      cout << "Input something: ";
      getline( cin >> ws, test );
      if ( isPalindrome( test ) ) cout << "Palindrome\n";
      else                        cout << string( test.rbegin(), test.rend() ) << '\n';
      cout << "Again? (y/n): ";   cin >> ans;
   }
}
Registered users can post here. Sign in or register to post.