fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string replace_num_with_x(const string &str);
  6.  
  7. int main() {
  8. cout << "Please enter a line of text:";
  9. string str;
  10. getline(cin, str);
  11. cout << replace_num_with_x(str);
  12. }
  13.  
  14. string replace_num_with_x(const string &str) {
  15. string word, result;
  16. string::size_type str_size = str.size(), start = 0, end;
  17.  
  18. do {
  19. end = str.find(' ', start);
  20. if (end == string::npos) {
  21. word = str.substr(start);
  22. start = str_size;
  23. }
  24. else {
  25. word = str.substr(start, end - start);
  26. start = end + 1;
  27. }
  28.  
  29. bool all_digits = true;
  30. string::size_type word_size = word.size();
  31.  
  32. for (string::size_type i = 0; i < word_size; ++i) {
  33. if (word[i] < '0' || word[i] > '9') {
  34. all_digits = false;
  35. break;
  36. }
  37. }
  38.  
  39. if (!result.empty()) {
  40. result += ' ';
  41. }
  42.  
  43. if (all_digits) {
  44. result.append(word_size, 'x');
  45. } else {
  46. result += word;
  47. }
  48. }
  49. while (start < str_size);
  50.  
  51. return result;
  52. }
Success #stdin #stdout 0s 4972KB
stdin
My userID is john17 and my 4 digit pin is 1234 which is secret
stdout
Please enter a line of text:My userID is john17 and my x digit pin is xxxx which is secret