使用C语言
我已经将类似-> aaaabbbbbbccccc的字符串压缩为a4b6c5,现在我想将a4b6c5解压缩为aaaabbbbbbccccc。我已经尝试过,但无法执行此减压操作。请帮我。

#include<stdio.h>
void compress(char*stng);

int main(){
    char stng[50000];
    printf("Enter the String :");
    scanf("%s",stng);
    compress(stng);
   return 0;
}

void compress(char*stng)
{

    int i=0,count=0;
    char c=stng[i];

    while(stng[i]!='\0')
    {
        if(c==stng[i])
            count++;
        else
        {
            printf("%c%d",c,count);  c=stng[i];  count=1;
        }
        i++;
    }

    printf("%c%d\n",c,count);
}

最佳答案

#include <stdio.h>

void decompress(const char* code)
{
    while(*code)
    {
        char c = *code++;   // Get the letter to be repeated
        int rep = 0;
        while(isdigit(*code))
        {
            rep = rep*10 + *code++ - '0';  // Get the number of times to repeat the letter
        }
        char set[rep];
        printf("%.*s", rep, memset(set, c, rep));  // Print letter [c], repeated [rep] times
    }
}


int main(void)
{
    char* code = "A4B5C6";
    decompress(code);
    return 0;
}

关于c - 压缩与解压缩,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58191611/

10-13 03:32