-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_replace.cc
More file actions
39 lines (37 loc) · 950 Bytes
/
string_replace.cc
File metadata and controls
39 lines (37 loc) · 950 Bytes
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
#include <iostream>
#include <cctype>
using namespace std;
int
main (int argc, char *argv[])
{
string sentence;
string key;
cout << "Enter the string" << endl;
std::getline(std::cin, sentence);
cout << "Enter the keyword to be hidden" << endl;
cin >> key;
for (auto &x: sentence)
{
x = std::tolower(x);
}
for (auto &x: key)
{
x = std::tolower(x);
}
std::size_t indx {};
while(true) {
indx = sentence.find(key, ++indx); // FIXME this may overflow
if (indx == std::string::npos) {
break;
}
if ((indx != 0) && isalpha(sentence[indx - 1])) {
continue;
}
auto end_indx = indx + key.length();
if ((end_indx != sentence.length()) && (isalpha(sentence[end_indx]))) {
continue;
}
sentence.replace(indx, key.length(), key.length(), '*');
};
cout << sentence << endl;
}