bool isReverse(string s1,string s2)
// pre : none
// post : returns true if s1 is reverse of s2 and otherwise false
{
	if (s1.length() != s2.length())
	{
		return false;
	}

	int size = s1.length();
	int count = 0;

	else
	{
		for (int i = 0; i < size; i++)
		{
			if (s1.at(i) == s2.substr(s2.length()-i-1,1)
			{	
				count++;
			}			
		}
	}

	if (count == size)
		return true;
	else
		return false;
}

no match for 'operator==' in '(&s1)->std::basic_string<_CharT, _Traits, _Alloc>::at [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((unsigned int)i)) == std::basic_string<_CharT, _Traits, _Alloc>::substr(typename _Alloc::size_type, typename _Alloc::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((unsigned int)((size - i) - 1)), 1u)'


pretty sure my logic is right?

Recommended Answers

All 9 Replies

You forgot the end brace: if (s1.at(i) == s2.substr(s2.length()-i-1,1)[B])[/B] >pretty sure my logic is right?
No, I would have used if (s1.at(i) == s2[i]) instead of the above :)

You forgot the end brace: if (s1.at(i) == s2.substr(s2.length()-i-1,1)[B])[/B] >pretty sure my logic is right?
No, I would have used if (s1.at(i) == s2[i]) instead of the above :)

the error is still coming up

if (s1.at(i) == s2.substr(s2.length()-i-1,1) ) is attempting to compare a character to a string. Even though you limit that substring to one character, it's still a string.

if (s1.at(i) == s2.substr(s2.length()-i-1,1) ) is attempting to compare a character to a string. Even though you limit that substring to one character, it's still a string.

wats the best way around this?

I think if (s1.at(i) == s2.at(s2.length()-i-1) ) is doing what you want. You're trying to compare the char at position i in s1 to the corresponding char starting from the back end of s2, right?

If it bothers you, try to be expressive while writing your code: Use iterators.
learn the reverse iterators at http://www.cplusplus.com/reference/string/string/rbegin/
After you read the example there, study the following code:

bool isrev(string s1,string s2)
{
    string::reverse_iterator rit;
    string::iterator it;
    if(s1.size()!=s2.size())
        return false;

    for ( rit=s1.rbegin(),it=s2.begin();
    rit < s1.rend()&&it<s2.end();
    rit++,it++ )
        if (*it!=*rit)
            return false;

return true;

}
bool isReverse(const std::string& s1, const std::string& s2)
{
    std::string t(s2);
    std::reverse(t.begin(),t.end());
    return s1 == t;
}

Good, but that requires <algorithm>
Anyways. OP never mentioned that he is restricted not to use it.
I was also thinking of sending this as solution. I dropped the plan when I realize that it would kill his purpose. :)

Apropos, it's possible to simplify for loop condition: no need to test both iterators because s1.size() == s2.size() there. Furthemore, const reference parameters are more effective (and provide practically the same conversions as string by value in that case).
By the way, I don't like <algorithm> based solutions ;)

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.