Whats wrong?

Keep asking the user for characters until the user enters #. The characters may be stored in a vector of chars or in a string. Do NOT include # in your list. If the series of characters is a palindrome, print out "Yes!". If the series of characters is NOT a palindrome, printout "No!" A palindrome is a word that can be read forwards and backwards the same. And example of a palindrome is "racecar".

How would i print No! if it isnt a palindrome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <string>

using namespace std;

int main()
{
    char input = ' ';
    string str;
    while (input != '#')
    {
        cin >> input;
        if (input != '#')
            str += input;
        
    }   
cout << "Yes!";
    
    system("pause");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string str ;

    char input ;
    while( std::cin >> input && input != '#' ) str += input ;

    std::cout << "string: " << str << '\n' ;

    // brute force
    const std::string rev_str( str.rbegin(), str.rend() ) ;
    std::cout << "palindrome? " << ( str==rev_str ? "yes" : "no" ) << '\n' ;

    // may be for later
    const bool palindrome = std::equal( str.begin(), str.begin()+str.size()/2, str.rbegin() ) ;
    std::cout << "palindrome? " << ( palindrome ? "yes" : "no" ) << '\n' ;

}
Topic archived. No new replies allowed.