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

Dani AI

Generated

Nice start, . Two quick iterator gotchas tripping you up: (1) use != to stop a reverse-iterator loop (it != line.rend()), not <. (2) Do not step to rend()+1; that is past the reverse range and is undefined to dereference. If you really want random access on the reversed view, you can index from the back via *(line.rbegin() + i) or store a reverse iterator and use rit[i]. Also prefer std::string::const_reverse_iterator when you are only reading. Finally, avoid naming a variable reverse; it shadows the standard algorithm name and makes code harder to read.

is right that you do not need to build a separate reversed string. A two-pointer pass is faster and simpler, and you can easily add common palindrome rules (ignore case and punctuation). Here is a drop-in helper that does exactly that:

#include <string>
#include <cctype>

bool is_palindrome(const std::string& s) {
    std::size_t i = 0, j = s.size();
    if (j == 0) return true;
    --j;
    while (i < j) {
        while (i < j && !std::isalnum(static_cast<unsigned char>(s[i]))) ++i;
        while (i < j && !std::isalnum(static_cast<unsigned char>(s[j]))) --j;
        if (i < j) {
            char a = static_cast<char>(std::tolower(static_cast<unsigned char>(s[i])));
            char b = static_cast<char>(std::tolower(static_cast<unsigned char>(s[j])));
            if (a != b) return false;
            ++i; --j;
        }
    }
    return true;
}

If you still want a one-liner using iterators, ’s idea of comparing the forward range to the reverse range is fine for exact, case-sensitive checks. And while mentioned std::reverse, keep in mind that actually mutates the string; use it on a copy if you go that route. Also note for completeness: C’s strrev mentioned earlier is non-standard and not portable in C++.

Recommended Answers

All 7 Replies

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.