c++ check if string contains substring
string str ("There are two needles in this haystack.");
string str2 ("needle");
if (str.find(str2) != string::npos) {
//.. found.
}
c++ check if string contains substring
string str ("There are two needles in this haystack.");
string str2 ("needle");
if (str.find(str2) != string::npos) {
//.. found.
}
c++ check substring
if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}
how to check substring in string c++
#include<iostream>
#include<ctype.h>
using namespace std;
int main() {
int a = 0;
bool b = 0;
string s, s1; cin >> s >> s1;
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < s1.length(); j++) {
if(a == s1.length()) b = 1;
if(s[i] == s1[j + a]) {
a++;
break;
}
else {
a = 0;
break;
}
}
}
if(a == s1.length() || b) cout << "YES";
else cout << "NO";
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us