Answer to Question #28784 in C++ for Syed Afzait

Question #28784
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
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS