I have a string like ...hello...world..., and I want to check if it is in some strings,so I split it by ... and save it in a <vector> string,the problem is how to check both of item in input_str sequentially?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string input_str1 = "Good morning, hello, a beautiful world";//true
string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true
//item = "...hello...world..."
vector<string> item;
item.push_back("hello");
item.push_back("world");
bool status = false;
for (auto sub_item : item)
{
cout << sub_item << ' ';
if (input_str2.find(sub_item) != string::npos) //change str1 to str2
status = true;
else
{
status = false;
break;
}
}
cout << status;
}
Checking input_str1 works fine, but for input_str2 the output should be 0 because the two words appear not in the right order. My code prints 1 for both.
bool status = true;and removestatus = true;. You start with the assumption that the string contains all words. Then, you iterate over the list of words and check each word. If a word doesn't exist, setstatus = false;. Don't set it back totrue.input_str2contains"hello"and"world"