64 693
Assignments Done
99,3%
Successfully Done
In September 2018

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 ?
Expert's answer
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions