fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string comp(string s)
  5. {
  6. char cur = 0;int c = 0;
  7. string res = "";
  8. for(int i=0;i<s.length();i++)
  9. {
  10. if(s[i] == cur)
  11. {
  12. c++;
  13. }
  14. else
  15. {
  16. if(c != 0)res.append(to_string(c));
  17. res.append(1,s[i]);
  18. c = 1;
  19. cur = s[i];
  20. }
  21. }
  22. res.append(to_string(c));
  23. return res;
  24. }
  25.  
  26. int main() {
  27. string s;
  28. cin >> s;
  29. cout << comp(s) << endl;
  30. return 0;
  31. }
Success #stdin #stdout 0s 15240KB
stdin
abcdef
stdout
a1b1c1d1e1f1