Advertisement
vencinachev

Zad1

Apr 19th, 2021
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. void stringCompress(char* result, const char *str)
  7. {
  8.     strcpy(result, "");
  9.     char numbuff[10];
  10.     char buff1[10];
  11.     int i = 0;
  12.     char current;
  13.     int cnt = 0;
  14.     while(*(str + i))
  15.     {
  16.         char current = str[i];
  17.         do
  18.         {
  19.             i++;
  20.             cnt++;
  21.         }
  22.         while (str[i] == current);
  23.         itoa(cnt, numbuff, 10);
  24.         cnt = 0;
  25.         sprintf(buff1, "%s%c", numbuff, current);
  26.         strcat(result, buff1);
  27.     }
  28. }
  29.  
  30. int main()
  31. {
  32.     char str[200];
  33.     stringCompress(str, "AAABBCCCC");
  34.     cout << str << endl;
  35.     stringCompress(str, "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWW");
  36.     cout << str << endl;
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement