运行结果:
Enter a string: My userID is john17 and my 4 digit pin is 1234 which is secret.
My userID is john17 and my x digit pin is xxxx which is secret.
replaceDigits.cpp
#include <iostream>
#include <string>
using namespace std;
void replaceDigits(string& s);
int main() {
string s;
cout << "Enter a string: ";
getline(cin, s);
replaceDigits(s);
cout << s;
return 0;
}
void replaceDigits(string& s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
while (isdigit(s[++i])) {
s[i] = 'x';
}
}
}
}