Skip to content

Instantly share code, notes, and snippets.

@hctilg
Created April 25, 2024 03:14
Show Gist options
  • Save hctilg/03a1f5ed2fcbe2c0d78e444731fcf448 to your computer and use it in GitHub Desktop.
Save hctilg/03a1f5ed2fcbe2c0d78e444731fcf448 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main() {
    string sentence, word;
    cout << "Enter a sentence: ";
    getline(cin, sentence);
    cout << "Enter a word: ";
    getline(cin, word);
    string highlight = string(sentence.length(), ' ');
    size_t found = sentence.find(word);
    while (found != string::npos){
        highlight.replace(found, word.length(), string(word.length(), '-'));
        found = sentence.find(word, found + 1);
        if (found == string::npos) {
            string result = "The word '" + word + "' was found in the sentence: ";
            cout << result << sentence << endl << string(result.length(), ' ') << highlight << endl;
        }
    }
    return 0;
}
@hctilg
Copy link
Author

hctilg commented Apr 25, 2024

preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment