Can you help me please?

Joined
Feb 2, 2023
Messages
3
Reaction score
0
Enter a sequence of sentences and a keyword. Identify sentences that contain a given keyword or display a message about the absence of such sentences.
 
Joined
Mar 5, 2023
Messages
36
Reaction score
12
since you didn't specify a program language i went with c++ let me know if you want it in a different one,

Code:
#include <iostream>
#include <string>


using namespace std;


int main() {
    string sentences, keyword;
    bool found = false;
   
    // Prompt the user to enter a sequence of sentences
    cout << "Enter a sequence of sentences: ";
    getline(cin, sentences);
   
    // Prompt the user to enter a keyword to search for
    cout << "Enter a keyword: ";
    cin >> keyword;
   
    // Tokenize the sentences by splitting them into substrings using the delimiter '.'
    size_t start = 0, end = 0;
    while ((end = sentences.find('.', start)) != string::npos) {
        // Get the current sentence
        string sentence = sentences.substr(start, end - start);
       
        // Check if the sentence contains the keyword
        if (sentence.find(keyword) != string::npos) {
            // Display the sentence that contains the keyword
            cout << sentence << "." << endl;
            found = true;
        }
       
        // Move to the next sentence
        start = end + 1;
    }
   
    // Check if the keyword was not found in any sentence
    if (!found) {
        cout << "The keyword was not found in any sentence." << endl;
    }
   
    return 0;
}

The program uses the getline() function to read a sequence of sentences from the user and the cin object to read a keyword. It then tokenizes the sentences by splitting them into substrings using the delimiter '.' and checks if each sentence contains the keyword using the find() function of the string class. If a sentence contains the keyword, it is displayed to the user. If the keyword was not found in any sentence, a message is displayed to that effect.

I hope this helps you :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Can you help me please? 1
Please, help me. 1
Cycling/changing message using JS 1
Help please 0
Help me 4
Please, help me. 1
Can't solve problems! please Help 0
help me with my code 1

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top