fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void compress(string input)
  7. {
  8. int counter=1;
  9. char lastChar=input[0];
  10.  
  11. for(int i=0; i<=input.length(); i++)
  12. {
  13. //cout << "DBG: " << input[i] << endl;
  14. if(input[i]==lastChar && i!=0)
  15. {
  16. lastChar=input[i];
  17. counter++;
  18.  
  19. if(i==input.length()-1)
  20. cout << counter << lastChar;
  21. }
  22. else // Combo broken
  23. {
  24. if(counter>1)
  25. cout << counter << lastChar;
  26. else
  27. cout << lastChar;
  28.  
  29. lastChar=input[i];
  30. counter=1;
  31. }
  32. }
  33. }
  34.  
  35. int main() {
  36.  
  37. compress("aabbc");
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
a2a2bc