fork download
  1. #include <iostream>
  2. using namespace std;
  3. string stringCompression(string A) {
  4. int index = 0; char character = ' '; int count = 1; string convertInt = ""; int initial_index = 0, length = A.length();
  5. while (index <A.length()) {
  6. character = A[index];
  7. initial_index = index;
  8. index++;
  9. while (index<=A.length() && A[index] == character)
  10. {
  11. count++;
  12. index++;
  13. }
  14. if (count > 1)
  15. {
  16. convertInt = to_string(count);
  17. A.erase((initial_index+1), count-1);
  18. A.insert(initial_index+1, convertInt);
  19. index = initial_index+convertInt.length()+1;
  20. count = 1;
  21. }
  22. }
  23. return A;
  24. }
  25. int main()
  26. {
  27. cout << stringCompression("")<<endl ;
  28. cout << stringCompression("abcde")<<endl ;
  29. cout << stringCompression("aabcccccaaa")<<endl;
  30. }
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
abcde
a2bc5a3