2

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.

15
  • Assuming I understand you correctly, you'll need two vectors. The first one containing your 'search phrases', the second one containing your 'phrases to be searched'. Once you've got that, it's a simple matter of doing two iterations and checking if the string has been found. Commented Jul 25, 2022 at 7:23
  • 2
    I don't understand your question. Can you clarify what you have as input, what you expect as output and what the problem is with the code you wrote already? Commented Jul 25, 2022 at 7:25
  • 1
    Initialize bool status = true; and remove status = 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, set status = false;. Don't set it back to true. Commented Jul 25, 2022 at 7:30
  • 1
    why ? input_str2 contains "hello" and "world" Commented Jul 25, 2022 at 7:34
  • 1
    not sequentially...llike I said in 1st paragraph @463035818_is_not_a_number Commented Jul 25, 2022 at 7:35

3 Answers 3

5

I'd suggest Regular Expressions if it's ok with your assignment. It'd look like this:

#include <iostream>
#include <vector>
#include <regex>

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..."
    const std::regex rxItems{"hello.*world"};
    const bool status = std::regex_search(input_str2, rxItems);

    cout << status;
}

This code searches for the strings "hello" and "world" in sequence, but accepts any characters (or even none) in between.

Sign up to request clarification or add additional context in comments.

Comments

2

std::string::find returns the position where the string was found and there is an overload that takes the position to start searching as paramter:

#include <iostream>
#include <vector>

using namespace std;

bool check(const std::string& input_str2) {
    vector<string> item;
    item.push_back("hello");
    item.push_back("world");
    size_t index = 0;
    for (auto sub_item : item)
    {
        cout << sub_item << ' ';
        index = input_str2.find(sub_item,index);
        if (index == string::npos) {
            return false;
        }
    }
    return true;
}


int main()
{
    string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true
    //item = "...hello...world..."
     cout << check(input_str2);
}  

Better start with status = true because if items is empty then finding 0 items is always successful. Then there is no need to assign true when a word is found and when placed in a function the variable can be removed alltogether.

Comments

1

Initialize bool status = true; and remove status = 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, set status = false;.

Store the position of the last result and start the next search at that position.

#include <iostream>
#include <vector>

int main()
{
    std::string input_str1 = "Good morning, hello, a beautiful world";//true
    std::string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true
    //item = "...hello...world..."
    std::vector<std::string> item;
    item.push_back("hello");
    item.push_back("world");
    bool status = true;
    std::string::size_type pos = 0;
    for (auto sub_item : item)
    {
        std::cout << sub_item << ' ';
        pos = input_str2.find(sub_item, pos);
        if (pos == std::string::npos)
        {
            status = false;
            break;
        }
    }
    std::cout << status;
}

Output

hello world 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.