fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4.  
  5. int main()
  6. {
  7. char input[] = "Some input with \t multiple \n spaces";
  8.  
  9. std::cout << "Length of string before: " << std::strlen(input) << '\n';
  10. std::cout << "input before: \"" << input << "\"\n";
  11.  
  12. auto last = std::unique(&input[0], input + std::strlen(input), [](char const& a, char const &b)
  13. {
  14. return std::isspace(a) && std::isspace(b);
  15. });
  16. *last = '\0'; // Terminate string
  17.  
  18. std::cout << "Length of string after: " << std::strlen(input) << '\n';
  19. std::cout << "last - input: " << (last - input) << '\n';
  20. std::cout << "input after: \"" << input << "\"\n";
  21. }
  22.  
Success #stdin #stdout 0s 4176KB
stdin
Standard input is empty
stdout
Length of string before: 37
input before: "Some input   with 	 multiple 
 spaces"
Length of string after: 31
last - input: 31
input after: "Some input with multiple spaces"