Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is a "Hello, world!" in C++ language by GCC for wandbox.
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using std::string;
- bool equalsChar(string::const_iterator& at1,
- string::const_iterator& at2,
- const char& ch,
- string::const_iterator& end1,
- string::const_iterator& end2)
- {
- if(at2 == end2) {
- if(at1 == end1)
- return true; // two empty strings are equal, right?
- if(*at1 == ch)
- return false;
- }
- else
- {
- if((*at1 != *at2) && (*at1 == ch || *at2 == ch)) return false;
- ++at2;
- }
- ++at1;
- return equalsChar( at1, at2, ch, end1, end2);
- }
- bool equalsChar(const string& strA, const string& strB, char ch)
- {
- // maybe do checks if strings are empty to shortcut the function?
- auto it1 = strA.begin(), it2 = strB.begin();
- decltype(it1) end1 = strA.end(), end2 = strB.end();
- if(strA.length() < strB.length())
- std::swap(it1,it2), std::swap(end1,end2);
- return equalsChar(it1, it2, ch, end1, end2 );
- }
- /*
- bool equalsChar(const string& strA, const string& strB, char ch) {
- if (strA.size() > strB.size()) { return equalsChar(strB, strA, ch); }
- auto common = [ch](char a, char b) { return (a == ch) == (b == ch); };
- return std::equal(strA.begin(), strA.end(), strB.begin(), common)
- && (std::find(strB.begin() + strA.size(), strB.end(), ch) == strB.end());
- }*/
- int main()
- {
- std::vector<std::pair<string,string>> data= {
- { "X", "X" },
- { "aaaXaaaX", "abcXcbaX" },
- { "XaXbXcX", "XtXoXpXdef" },
- { "XaXbXcX", "XtXoXpXdXf" },
- { "XXXX", "XX" },
- { "aXaXbXcX", "XtXoXpX" },
- {"werwer","werwerr"}};
- for(auto& item : data)
- std::cout << "equalsChar( " << item.first << ", " << item.second << " )"
- << string{ equalsChar(item.first,item.second,'X') ? " is true" : " is false"} << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement