Compare begin() str1 to rbegin() str2

I am trying to compare, the beginning of str1 to the end(inward) of str2.
For ex:

str1 = "hello there";
str2 = "ereht olleh";

So is i test, they sound be true given the above strings.

I have tried the blow:
if (str.begin() == str2.rbegin())
and
if (str.compare(str2.rend()) != 0)

But it is not working, looking for the correct format...
Iterators behave like pointers. To get to the element they point to, you have to apply the dereference operator *.

if (*str.begin() == *str2.rbegin())

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

void sayIfMirrored(const std::string &s1, const std::string &s2)
{
    std::string::const_iterator ci = s1.begin();
    std::string::const_reverse_iterator cri = s2.rbegin();

    std::cout << s1 << "\n\tand\n" << s2;

    for (; ci != s1.end() && cri != s2.rend(); ++ci, ++cri)
        if (*ci != *cri) // dereference the iterators, getting to letters
        {
            std::cout << "\n\tare NOT mirrored!\n\n" << std::endl;
            return;
        }

    std::cout << "\n\tare mirrored!\n\n" << std::endl;
}

int main()
{
    std::string s1 = "hello there";
    std::string s2 = "ereht olleh";
    std::string s3 = "Granite";

    sayIfMirrored(s1, s2);
    sayIfMirrored(s2, s1);
    sayIfMirrored(s1, s3);
}


Be advised, if you want to be more proficient, look into std::mismatch():
http://cplusplus.com/reference/algorithm/mismatch/

Edit: also my code has a bug. It will say "mirrored" for strings like s1 = "track"; and s2 = "cart"; because it stops checking as soon as the shortest ends.

Edit 2: http://cplusplus.com/reference/algorithm/equal/
Totally missed that.
Last edited on
Very helpful information. Thank you!
std::string s1 = "hellothere";
std::string s2 = "ereht olleh";
std::string s3 = "Granite";

How could you do the same as above, and ignore spaces, or better, all non alpha characters?

There are library solutions..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <ctype.h>
#include <boost/range/algorithm/equal.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/reversed.hpp>

bool f(const std::string& s1, const std::string& s2)
{
    using namespace boost::adaptors;
    return equal(s1 | filtered(::isalpha),
                 s2 | filtered(::isalpha) | reversed);
}
int main()
{

    std::string s1 = "hellothere";
    std::string s2 = "ereht olleh";
    std::string s3 = "Granite";

    std::cout << std::boolalpha
              << " comparing " << s1 << " and " << s2
              << " the result is " << f(s1, s2) << '\n'
              << " comparing " << s1 << " and " << s3
              << " the result is " << f(s1, s3) << '\n';
}

I get:
 comparing hellothere and ereht olleh the result is true
 comparing hellothere and Granite the result is false

Last edited on
Cant use boost for school work. I like boost but have to wait
Topic archived. No new replies allowed.