Code works, however i'm having problems with reverse string- i cant figure out how to index the elements. for cycle returns errors, what else could i use?
The end goal would be to compare string line with string reverse. If adequate elements match its a palindrome.

#include <iostream>
#include <string>
using namespace std;

int main()
    string line;
    cout<< "sentence: \n";
    getline (cin, line);

    cout<< "in reverse: \n";
    string::reverse_iterator reverse;
    for (reverse=line.rbegin(); reverse<line.rend(); reverse++)
    cout<< *reverse;

return 0;
9 Years
Discussion Span
Last Post by rohitvipin

There is a strrev() function in string.h for char array. I dont know enough to help you with your code but I suggest you to follow a different method for checking palindrome in a string.

Instead of checking a string with a reverse string, make two pointers one at the starting of the string and other at the end. For a string to be a palindrome the starting should match the end, if so then increment the starting pointer and decrease the end pointer. Again both should be equal for the string to be a palindrome. Keep doing so until start equals end.


To check if an iterator has passed over all elements compare it to the end (for iterators) or rend (for reverse iterators).

string::reverse_iterator rit = st.rbegin();
   string::iterator it = st.begin();
   for(; rit != st.rend() && it != st.end(); rit++, it++){
// do comparisons here
Votes + Comments
thanks for the help

for c++ there is an inbuilt function to reverse a string just give
you will get it

Votes + Comments
was exaclt what im looking

Thanks people, specially Arpy Giri for suggesting such an easy way out

using namespace std;
void main()
	string str;
	cout<<"Enter the string: ";
	string::reverse_iterator ri=str.rbegin();	//reverse iterator used to read the string from back to front
	if(equal(str.begin(),str.end(),ri))			//equal compares 2 strings in the provided order
		cout<<str<<" is a Pallindrome!"<<endl;
		cout<<str<<" is not a Pallindrome!"<<endl;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.