C语言字符串重新整合简写,不知道怎么写合适

将字符串中的重复字符个数简写,1需要不写

输入第一行是 字符串的个数,下面几行是字符串

输入
2
AABBCC
AABBCCDEAAA

输出:
2A2B2C
2A2B2CDE3A


#include <stdio.h>
#include <string.h>
int main(){
    int i,j,k,n;
    scanf("%d", &n);
    getchar();
    char str[n][256];
    for(i = 0;i < n;i++){
        gets(str[i]);
    }
    for(i = 0;i < n;i++){
        for(j = 0;j < strlen(str[i]) - 1;j++){
            int cnt = 1;
            if(str[i][j] == str[i][j+1]){
                char temp = str[i][j];
                while(str[i][j+1] == temp && j+1 < strlen(str[i])){
                    cnt ++;
                    j ++;
                }
                printf("%d%c",cnt,temp);
            }else{
                printf("%c",str[i][j]);
            }
            if(j == strlen(str[i]) - 2 && str[i][j] != str[i][j+1]){
                printf("%c",str[i][j+1]);
            }
        }
        printf("\n");
    }
    return 0;
}

#include <stdio.h>
#include <string.h>

// 交换两数
void swap(char *a, char *b) {
    char t = *a;
    *a = *b, *b = t;
}

// 逆置
void reverse(char *a, char *b) {
    while (a < b) {
        swap(a++, --b);
    }
}


// 压缩
int compress(char *chars, int charsSize) {
    int write = 0, left = 0;
    for (int read = 0; read < charsSize; read++) {
        if (read == charsSize - 1 || chars[read] != chars[read + 1]) {
            chars[write++] = chars[read];
            int num = read - left + 1;
            if (num > 1) {
                int anchor = write;
                while (num > 0) {
                    chars[write++] = num % 10 + '0';
                    num /= 10;
                }
                reverse(&chars[anchor], &chars[write]);
            }
            left = read + 1;
        }
    }
    return write;
}

int main()
{
        int times = 0;
        scanf("%d",&times);

        while(times--)
        {
                char str[1024] = "";
                scanf("%s",str);

                // 压缩
                compress(str, strlen(str));

                // 输出结果
                printf("\n%s", str);
        }
        printf("\n");

        return 0;
}

img