Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- char* stringCompress(const char *str)
- {
- char *result = new char[1];
- result[0] = '\0';
- char numbuff[10]; // buffer for number (count)
- char buff[11]; // buffer for number + char
- char current;
- int cnt = 0;
- int i = 0;
- while(*(str + i))
- {
- char current = str[i];
- do
- {
- i++;
- cnt++;
- }
- while (str[i] == current);
- itoa(cnt, numbuff, 10);
- cnt = 0;
- sprintf(buff, "%s%c", numbuff, current);
- char *b = result;
- result = new char[strlen(result) + strlen(buff) + 1];
- strcpy(result, b);
- delete[] b;
- strcat(result, buff);
- }
- return result;
- }
- int main()
- {
- char* text1 = stringCompress("AAABBCCCC");
- cout << text1 << endl;
- char* text2 = stringCompress("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWW");
- cout << text2 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement