write a c++ program that read a line from cin prints its characters in reverse order on cout and tells the given line is palindrome or not ?
1
Expert's answer
2013-04-23T08:23:51-0400
#include <iostream> #include <string>
using namespace std;
void main() { cout << "Please, enter your string:\n"; string line; // initialize string getline (cin, line); // read string for ( int i = line.length() - 1; i >= 0; --i) // print in reverse order { & cout << line [i]; } bool flag = 1; // flag for being a palindrome for ( int i = 0; i < line.length() / 2; ++i) { & if (line[i] != line[line.length() - 1 - i]) // if letters are different & { flag = 0; // isn't a palindrome break; // finish cycle & } } cout << ( flag ? "\nLine is a palindrome.\n" : "\nLine isn't a palindrome.\n"); // print result }
Comments
Leave a comment