#include <iostream>
#include <string>
bool match_positions(const std::string& str, int p1, int p2) {
int wordNo = 1;
size_t beg = 0, pos;
std::string first, second;
while ((pos = str.find('-', beg)) != std::string::npos ||
(first.empty() && second.empty()) ) {
if (wordNo == p1)
first = str.substr(beg, pos - beg);
if (wordNo == p2)
second = str.substr(beg, pos - beg);
beg = pos + 1;
++wordNo;
}
if (first.empty() || second.empty())
return false;
else
if (!first.compare(second))
return true;
else
return false;
}
int main() {
std::string text = "I-am-logged-into-StackOverFlow-I-am-using-StackOverFlow-";
std::cout << match_positions(text, 5, 9) << std::endl;
std::cout << match_positions(text, 5, 4) << std::endl;
std::string text2 = "I-am-reading-a-book-A-I-have-written-a-ANewbook-B-";
std::cout << match_positions(text2, 5, 11) << std::endl;
std::cout << match_positions(text2, 1, 7) << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKYm9vbCBtYXRjaF9wb3NpdGlvbnMoY29uc3Qgc3RkOjpzdHJpbmcmIHN0ciwgaW50IHAxLCBpbnQgcDIpIHsKCWludCB3b3JkTm8gPSAxOwoJc2l6ZV90IGJlZyA9IDAsIHBvczsKCXN0ZDo6c3RyaW5nIGZpcnN0LCBzZWNvbmQ7Cgl3aGlsZSAoKHBvcyA9IHN0ci5maW5kKCctJywgYmVnKSkgIT0gc3RkOjpzdHJpbmc6Om5wb3MgfHwKCQkoZmlyc3QuZW1wdHkoKSAmJiBzZWNvbmQuZW1wdHkoKSkgKSB7CgkJaWYgKHdvcmRObyA9PSBwMSkKCQkJZmlyc3QgPSBzdHIuc3Vic3RyKGJlZywgcG9zIC0gYmVnKTsKCQlpZiAod29yZE5vID09IHAyKQoJCQlzZWNvbmQgPSBzdHIuc3Vic3RyKGJlZywgcG9zIC0gYmVnKTsKCQliZWcgPSBwb3MgKyAxOwoJCSsrd29yZE5vOwoJfQoJaWYgKGZpcnN0LmVtcHR5KCkgfHwgc2Vjb25kLmVtcHR5KCkpCgkJcmV0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