Check input for one word

Hi, i want to write a program which gives out "Write any sentence" for example. Thats no problem of course. But is there any way that i can check the input of the user for one specific word? For example lets say the word is "test". So if the user puts in "example test" something should change. But if he only puts in "example" the program should run the normal way.
The following uses stringstreams to the split the sentence into its whitespace-delimited tokens. This could also be done with string operations (find, substr), but the stringstream way of doing it is pretty straightforward.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Example program
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

bool contains_word(const string& sentence, const string& word_to_find)
{
    // warning: basic logic; does not filter out capitalization, punctuation
    string word;
    istringstream iss(sentence);
    
    while (iss >> word)
    {
        if (word == word_to_find)
        {
            return true;   
        }
    }
    return false;
}

int main()
{
    const string special_word = "test";
    
    cout << "Write any sentence: ";
    string sentence;
    getline(cin, sentence);
    
    if (contains_word(sentence, special_word))
    {
        cout << "Special logic!\n";   
    }
    else
    {
        cout << "Normal logic!\n";   
    }
}
Thank you this helped me out a lot!
Topic archived. No new replies allowed.