Created
April 25, 2024 03:14
-
-
Save hctilg/03a1f5ed2fcbe2c0d78e444731fcf448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Author
hctilg
commented
Apr 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment