Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to check if all word in a vector exists in a given string with C++

1 Answer

0 votes
#include <iostream>
#include <vector>

// Function to check if all words exist in string s
bool allWordsExist(const std::string &s, const std::vector<std::string> &words) {
    for (const std::string &w : words) {
        if (s.find(w) == std::string::npos) {
            std::cout << "Word \"" << w << "\" does NOT exist in string s\n";
            return false; // early exit if any word is missing
        } else {
            std::cout << "Word \"" << w << "\" exists in string s\n";
        }
    }
    
    return true;
}

int main() {
    std::string s = "efandabandcd";
    std::vector<std::string> words = {"ab", "cd", "ef"};

    if (allWordsExist(s, words)) {
        std::cout << "All words exist in the string s\n";
    } else {
        std::cout << "Not all words exist in the string s\n";
    }
}



/*
run:

Word "ab" exists in string s
Word "cd" exists in string s
Word "ef" exists in string s
All words exist in the string s

*/

 



answered Dec 3, 2025 by avibootz
...