fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. string replace_num_with_x(const string &str);
  7.  
  8. int main() {
  9. cout << "Please enter a line of text:";
  10. string str;
  11. getline(cin, str);
  12. cout << replace_num_with_x(str);
  13. }
  14.  
  15. string replace_num_with_x(const string &str) {
  16. istringstream str_strm(str);
  17. string word, result;
  18.  
  19. while (str_strm >> word) {
  20. bool all_digits = true;
  21. string::size_type word_size = word.size();
  22.  
  23. for (string::size_type i = 0; i < word_size; ++i) {
  24. if (word[i] < '0' || word[i] > '9') {
  25. all_digits = false;
  26. break;
  27. }
  28. }
  29.  
  30. if (!result.empty()) {
  31. result += ' ';
  32. }
  33.  
  34. if (all_digits) {
  35. result.append(word_size, 'x');
  36. } else {
  37. result += word;
  38. }
  39. }
  40.  
  41. return result;
  42. }
Success #stdin #stdout 0s 4720KB
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