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;
}

Try line.rend()+1;

could you illustrate that on my example? :)

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
commented: thanks for the help +1

for c++ there is an inbuilt function to reverse a string just give
std::reverse(string_name.begin(),string_name.end())
you will get it

commented: was exaclt what im looking +1

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

#include<iostream>
#include<string>
using namespace std;
void main()
{
	string str;
	cout<<"Enter the string: ";
	cin>>str;
	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;
	else
		cout<<str<<" is not a Pallindrome!"<<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.