压缩前的数据: aaaacc333bkkkggggd
压缩后的数据: a|4c|23|3b|1k|3g|4d|1
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include
- #include
- #include
- #include
-
- void fun(char s[])
- {
- int i, j, n, c, k;
- char ch;
- for (i = 0; s[i]; i++)
- {
- c = 1;
- ch = s[i];
- for (j = i + 1; s[j]; j++)
- {
- if (ch != s[j])
- {
- break;
- }
- c++;
- }
- j = j - 1;
- /**********Program**********/
- if (c == 1) {
- //右移2
- for (k = strlen(s); k > j; k--)
- s[k + 2] = s[k];
- s[i] = ch;
- s[i + 1] = '|';
- s[i + 2] = '0' + c;
- i = j + 2;
-
- }
- else if (c == 2) {
- //右移1
- for (k = strlen(s); k > j; k--)
- s[k + 1] = s[k];
-
- s[i] = ch;
- s[i+1] = '|';
- s[i+2] = '0' + c;
- i = j+1;
-
-
- }
- else if(c == 3) {
- //不移
- s[i] = ch;
- s[i + 1] = '|';
- s[i + 2] = '0' + c;
- i = j;
-
- }
- else {
- //不移,直接拼接
-
- s[i] = ch;
- s[i + 1] = '|';
- s[i+2] = '0' + c;
- s[i+3] = '\0';
-
- i = strlen(s)-1;
- strcat(s, &s[j+1]);
-
- }
-
- /********** End **********/
- }
- }
- int main()
- {
- char s[100] = "aaaacc333bkkkggggd";
- printf("压缩前的数据: ");
- puts(s);
- fun(s);
- printf("压缩后的数据: ");
- puts(s);
- return 0;
- }