fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::size_t countSpaces(const std::string& s)
  5. {
  6. std::size_t spaces = 0;
  7.  
  8. for (auto letter : s)
  9. if (letter == ' ')
  10. ++spaces;
  11.  
  12. return spaces;
  13. }
  14.  
  15. std::string withoutSpaces(const std::string& s)
  16. {
  17. std::string result;
  18.  
  19. for (auto letter : s)
  20. if (letter != ' ')
  21. result += letter;
  22.  
  23. return result;
  24. }
  25.  
  26. int main()
  27. {
  28. std::string line;
  29.  
  30. // get the string!
  31. std::cout << "Enter a string\n";
  32. std::getline(std::cin, line);
  33.  
  34. std::cout << '"' << line << "\" contains " << countSpaces(line) << " spaces.\n";
  35. std::cout << "Without spaces: \"" << withoutSpaces(line) << "\"\n";
  36. }
Success #stdin #stdout 0s 3432KB
stdin
Well, it is no secret that the best thing about secrets is secretly telling someone your secret, thereby adding another secret to your collection of secrets, secretly.
stdout
Enter a string
"Well, it is no secret that the best thing about secrets is secretly telling someone your secret, thereby adding another secret to your collection of secrets, secretly." contains 26 spaces.
Without spaces: "Well,itisnosecretthatthebestthingaboutsecretsissecretlytellingsomeoneyoursecret,therebyaddinganothersecrettoyourcollectionofsecrets,secretly."