【问题标题】:C - function that compresses charactersC - 压缩字符的函数
【发布时间】:2018-08-13 17:16:45
【问题描述】:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char * compress(char *input, int size){

    char *inputa;
    char compressedString[100];
    inputa = (char*)malloc(sizeof(size));
    snprintf (inputa, size, "%s", input);
    int i = 0;
    int x;
    int counter;

    while(i < size){
        counter = 1;
        x = i;
        while (inputa[x] == inputa[x + 1] && (x+1) < size){
            x++;
            counter++;
        }
        if (i != x){
            i = x;
        }else{
            i++;        
        }
    }
    return inputa;
}

main(){

    char ez[] = "blaablaaa";

    printf("%s \n", compress(ez, sizeof(ez)));
    printf("%s", ez);

    return 0;
}

所以,我正在尝试制作这个压缩连续字符的函数(例如,"blaablaaa""bla2bla3")。我的想法是将inputa[x] 放在压缩数组上,然后放在计数器旁边,但我似乎无法让它工作。

【问题讨论】:

标签: c arrays memory-management


【解决方案1】:

让我们看看这两行:

inputa = (char*)malloc(sizeof(size));
snprintf (inputa, size, "%s", input);

size 的类型为int,所以sizeof(size) 是一个整数的大小,大概是4。

您使用malloc 分配了 4 个字节。

然后您使用 snprintf 尝试将所有输入(blaablaaa,10 字节长)复制到仅 4 字节长的缓冲区中。

10 字节不适合 4 字节缓冲区。

我不确定你想在那里做什么,但它不正确。

【讨论】:

  • 我试图分配 100 个字符,但似乎完全错误。
【解决方案2】:

1) 您分配的缓冲区太短:

inputa = (char*)malloc(sizeof(size)); 

它只分配 4 个字节。 你需要

inputa = (char*)malloc(sizeof(char)*size + 1 )); 

2) 你忘记释放分配的内存了。

3) 算法本身需要改进。代码中的注释:

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

/* reverse:  reverse string s in place */
void reverse(char s[])
{
     int i, j;
     char c;

     for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
         c = s[i];
         s[i] = s[j];
         s[j] = c;
     }
}
/* itoa is not a standard function */ 
/* itoa:  convert n to characters in s */
void itoa1(int n, char s[])
{
     int i, sign;

     if ((sign = n) < 0)  /* record sign */
         n = -n;          /* make n positive */
     i = 0;
     do {       /* generate digits in reverse order */
         s[i++] = n % 10 + '0';   /* get next digit */
     } while ((n /= 10) > 0);     /* delete it */
     if (sign < 0)
         s[i++] = '-';
     s[i] = '\0';
     reverse(s);
}

char * compress(char *input, int size){
   int i = 0;
   int r;          // number of repetitions
   char add[2];    // current character buffer
   char rep[32];   // repetitions buffer
   char c;         // current character    

   char *compr = (char* )malloc(sizeof(char)*size + 1); // memory for the compressed string
   compr[0] = 0;   // terminate the buffer
   add[1] = 0;     // terminate the buffer

    while(i < size){

         c = add[0] = input[i]; // get a character 
         strcat(compr,add);     // add to compr
         r = 1;                 // default number of repetitions is one

         while(1)   // count and add to the string
         {
            if(c == input[i+1] ) 
            {        // find how many characters follows c
                r++; // number of repetition
                i++; // moving along the input buffer
            }
            else
            {
                //  check the r for number of repetitions
                if( r > 1)
                {
                    // there were repetitions:
                    // char * itoa ( int value, char * str, int base );
                    itoa1(r,rep);          // get the number 
                    strcat(compr,rep);     // add  repetition number to the compressed string
                }

                i++;// advance to the next character
                break;
            } // else

         }// while
     } //while

    return compr;
}

int main(void){

    char sg7[] = "BLaaaBBLLaaaaXXXaaY";
    char ez[] = "blaablaaa";
    char *ptr;

    printf("%s \n", ptr = compress(sg7, strlen(sg7) ) );
    printf("%s \n", sg7);
    free(ptr);

    printf("\n");

    printf("%s \n", ptr = compress(ez, strlen(ez)));
    printf("%s \n", ez);
    free(ptr);

    return 0;
}

输出:

BLa3B2L2a4X3a2Y 
BLaaaBBLLaaaaXXXaaY 

bla2bla3 
blaablaaa 

希望对你有帮助。

【讨论】:

  • @George P. 如果您有任何问题或需要更多帮助,请在 cmets 中告诉我。
猜你喜欢
  • 1970-01-01
  • 2011-11-12
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
相关资源
最近更新 更多