Answers for "how to check substring in string c++"

C++
4

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.
}
Posted by: Guest on December-23-2020
0

c++ check substring

if (s1.find(s2) != std::string::npos) {
    std::cout << "found!" << '\n';
}
Posted by: Guest on July-07-2020
0

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;
}
Posted by: Guest on October-28-2021

Code answers related to "how to check substring in string c++"

Browse Popular Code Answers by Language