+ 4
Manav Roy, Something like this bro? https://code.sololearn.com/cEC01k52TVr4/?ref=app
14th Jun 2022, 7:01 AM
Ipang
+ 2
Ipang nice. 😎 C++ methods are not given enough love. Mainly because people fall back to C type solutions. Maybe because teaching materials do not introduce them to students.
14th Jun 2022, 12:13 PM
Bob_Li
Bob_Li - avatar
+ 1
Use find method.. https://m.cplusplus.com/reference/string/string/find/ edit: you can use this function to check as you needed by using a loop.. I think, there is no predined function which about you searching ... Not sure, if you find let me know... hope it helps....
13th Jun 2022, 4:50 PM
Jayakrishna 🇮🇳
+ 1
Manav Roy see if strpbrk does what you want. #include <cstring> //needed for strpbrk https://www.tutorialspoint.com/c_standard_library/c_function_strpbrk.htm
13th Jun 2022, 7:07 PM
Brian
Brian - avatar
+ 1
Manav Roy, You can try find_first_not_of() method of std::string to check whether all characters in <prt> has a match in <str>. https://en.cppreference.com/w/cpp/string/basic_string/find_first_not_of
14th Jun 2022, 5:18 AM
Ipang
+ 1
Manav Roy errors are eliminated after a few adjustments... #include <iostream> #include <cstring> using namespace std; int main() { string a="hello"; string b="bell"; const char *c= strpbrk(a.c_str(),b.c_str()); cout << c; return 0; } Output: ello
14th Jun 2022, 6:43 AM
Brian
Brian - avatar
+ 1
for(auto i:prt){ if(std::find(str.begin(), str.end(), i) == str.end()) return 0; } return 1;
14th Jun 2022, 12:56 PM
Cosm07
Cosm07 - avatar
+ 1
Manav Roy Some thing like this, you can use find method : #include <string> #include <iostream> int main() { using std::string, std::cout; bool match=true; string str { "Pegasus" }, prt { "eas" }; for(int i=0; i< (int)prt.length(); i++) if(string::npos == str.find(prt[i])) { cout << "Some characters in '" << str << "' has no match in '" << prt <<"'"; match=false; } if( match ) cout << "All characters in '" << str << "' has a match in '" << prt<<"'"; return 0; }
14th Jun 2022, 1:25 PM
Jayakrishna 🇮🇳
+ 1
Whenever working with strings Regex is a powerful tool to consider using. See how to use it in C++: https://www.softwaretestinghelp.com/regex-in-cpp/
15th Jun 2022, 10:09 AM
Adam McGregor
+ 1
Adam McGregor regex is always an option.But it is trickier. Plus, writing good regex is an artform in itself... https://www.loggly.com/blog/regexes-the-bad-better-best/
15th Jun 2022, 11:38 AM
Bob_Li
Bob_Li - avatar
+ 1
https://www.sololearn.com/Discuss/3046857/?ref=app
15th Jun 2022, 3:03 PM
Sokkai
Sokkai - avatar
0
You could parse prt string and for each character c in prt call strchr(str,c). Have a bool variable that is initialized with value true and becomes false if one of strchr calls doesnt find your character
14th Jun 2022, 10:41 AM
Stefan Corneteanu
Stefan Corneteanu - avatar
0
Manav Roy the reason that your test of strpbrk printed "ello" is because c pointed to the first character in a that matched any of b. Question: Your example shows the letters matching if they share the same sequence in order - 'e', 'a', 's'. What if prt is rearranged to "sea"? Is it still a match? If the answer is no - the letters must be in the same order, then what you are looking for is the algorithm known as the Longest Common Subsequence. (Note: This not to be confused with finding the Longest Common Substring, which requires the letters to be adjacent as well as in order.)
14th Jun 2022, 12:03 PM
Brian
Brian - avatar
0
Manav Roy a variation of Ipang 's solution. #include <string> #include <iostream> int main() { using std::string, std::cout; string str {"Pegasus"}, prt {"sea"}; for(auto c:prt){ str.find(c); if(str.find(c)==size_t(-1)){ cout<<"Some characters in '"<<prt<<"' has no match in '"<<str<<"'"; return 0; } } cout<<"All characters in '"<<prt<<"' has a match in '"<<str <<"'"; return 0; }
14th Jun 2022, 3:33 PM
Bob_Li
Bob_Li - avatar