c++ - Finding substring not working -
this code supposed return yes if string has 'nadia' no otherwise. when submitted said wrong answer although wokring on sample tests.? know missing?
#include<iostream> #include<string> #include<vector> using namespace std; int main() { int t; bool flag = false; bool flag2 = false; bool flag3 = false; bool flag4 = false; int count = 0; vector<string>v; cin >> t; string x; (int = 0; < t; i++) { cin >> x; v.push_back(x); } (int = 0; < v.size(); i++) { x = v[i]; (int = 0; < x.length(); i++) { if (x.at(i) == 'n') { flag = true; (int = 0; < x.length(); i++) { if (x.at(i) == 'a') { ++count; flag2 = true; (int = 0; < x.length(); i++) { if (x.at(i) == 'd') { flag3 = true; (int = 0; < x.length(); i++) { if (x.at(i) == 'i') { flag4 = true; } } } } } } } } if ((flag) && (flag2) && (flag3) && (flag4) && (count >= 2)) { cout << "yes" << endl; } else { cout << "no" << endl; } count = 0; flag = false; flag2 = false; flag3 = false; flag4 = false; } system("pause"); return 0; }
sample input:
3 anhaldillooa nnaaddiiaa nxzdiao
sample output:
yes yes no
you're building state machine find substrings.
std::string str ("there 2 needles in haystack needles."); std::string str2 ("needle"); // different member versions of find in same order above: std::size_t found = str.find(str2); if (found!=std::string::npos) std::cout << "first 'needle' found at: " << found << '\n';
Comments
Post a Comment