+ 4
Manav Roy,
Something like this bro?
https://code.sololearn.com/cEC01k52TVr4/?ref=app
+ 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.
+ 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....
+ 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
+ 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
+ 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
+ 1
for(auto i:prt){
if(std::find(str.begin(), str.end(), i) == str.end()) return 0;
}
return 1;
+ 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;
}
+ 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/
+ 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/
+ 1
https://www.sololearn.com/Discuss/3046857/?ref=app
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
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.)
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;
}