Checking if two strings are mirror of each other

Ok so I am trying to check if two words are mirror of each other. For example, string1 = abc and string2 = cba they are mirror of each other.
The 1st loop is for string1 and 2nd loop string2. I compare the first char of string 1 to the last char of string 2 and then work my way in. Still trying to find a way to make it simpler than this if possible.

1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < length1 - 1; i++)
  {
	for (int j = length2 - 1; j > 0; --j)
	{
		if (string1[i] == string2[j])
		{
			return true;
		}

	}
  }
You can use some of what C++ provides:

1
2
3
4
5
std::reverse(string1.begin(), string1.end());
if (string1 == string2)
{
  return true;
}
closed account (48T7M4Gy)
Why two loops and not just one?

If string1 = "abc," string2 = "cba", then string1[2] == string[0]

If you combine that with the length of the string then the single loop will do.
You can also go one step further and use begin(), end() functionality in the same single only loop structure.

See: http://www.cplusplus.com/reference/string/basic_string/begin/
Thank you for the help!
Topic archived. No new replies allowed.