-3
#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;
}

So, I am trying to make this function that compresses consecutive characters (eg. "blaablaaa" to "bla2bla3"). My thought process is to put the inputa[x] on the compressed array and next to it the counter, but I can't seem to make it to work.

Bugs
  • 4,491
  • 9
  • 32
  • 41
George P.
  • 9
  • 1
  • 1
    Please see https://stackoverflow.com/help/how-to-ask and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ . " can't seem to make it to work." is not a very useful description of a problem. –  Mar 05 '18 at 22:56
  • Possible duplicate of [Compression Program in C](https://stackoverflow.com/questions/19748991/compression-program-in-c) – Mark Benningfield Mar 05 '18 at 22:59

2 Answers2

0

Lets take a look at these two lines:

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

size has type int, so sizeof(size) is the size of an integer, which is probably 4.

You used malloc to allocate 4 bytes.

Then you use snprintf to try to copy all of your input (blaablaaa, 10-bytes long) into a buffer that is only 4 bytes long.

10 bytes won't fit into a 4 byte buffer.

I'm not sure what you're trying to do there, but it is not correct.

abelenky
  • 63,815
  • 23
  • 109
  • 159
0

1) Your allocated buffer was too short:

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

It allocates only 4 bytes. You needed

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

2) You forgot to release the allocated memory.

3) The algorithm itself needed the improvements. Comments in the code:

#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;
}

Output:

BLa3B2L2a4X3a2Y 
BLaaaBBLLaaaaXXXaaY 

bla2bla3 
blaablaaa 

I hope it helps.

sg7
  • 6,108
  • 2
  • 32
  • 40
  • @George P. If you have any questions or need more help let me know in the comments. – sg7 Mar 07 '18 at 15:18