How to unpack a char array in c

I need to create a function:

char * decompress(const char * src) {
}

      

The input to this function is some line:

Hello world! -> Hello world!

Hel2o world!10" -> Hello world!!!!!!!!!!

      

as you can see if there is some number, it repeats the previous char that number of times. I am a java programmer but now I need to solve this with c;

I have it now. It just prints, I hope the right value, but I don't know how to assign it to return a pointer

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

char * decompress(const char * src) {
    int max = 0;
    int pom = 1;

    char *vysledek = "";
    int i;
    for (i = 0; i < strlen(src); i++) {
        max = 0;
        pom = 1;

        while (isdigit(src[i])) {
            int digit = (src[i] - '0');
            max = max * 10 + digit;
            i++;
            pom++;
        }

        if (max == 0) {
            max = 1;
        }

        int j;
        for (j = 0; j < max; j++) {
            printf("%c", src[i - pom]);
        }
    }

    return vysledek;
}

int main(int argc, char * argv []) {

    decompress("Hel2o world!10");

    return 0;
}

      

+3


source to share


1 answer


Since you mentioned that you are a Java programmer, you should learn how C memory assignments (in this case especially strings) work:

char *vysledek = "";

      

In Java, this will create a string object in which you can simply add characters as you wish. In C, however, this will basically create an array char[1]

containing '\0'

(line terminator).

Since - like Java arrays - you cannot (should not) write outside the bounds of an array, you can store one character in that array, which is not enough for your function.

You can find out how much memory you'll have to allocate by iterating over the input first (as Neath mentioned in his comment). Then you can malloc

create the necessary memory for your output pointer:

vysledek = malloc (<output string length>);

      


Now that you've allocated enough memory, you can write your output to this array, so instead of



printf("%c", src[i - pom]);

      

now you can write:

vysledek[vysledek_counter++] = src[i - pom];

      

and return a pointer.


Note that after you are done using it, you must free the return value, even if it is at the end of the main method, which should look something like this:

int main(int argc, char * argv []) {
    char *output;

    output = decompress("Hel2o world!10");
    printf("%s\n", output);
    free(output);

    return 0;
}

      

One more note: your code won't work if it encounters an endless string (i.e. the character '\0'

doesn't indicate the end of the string). To counter this, pass an extra string length parameter (you will need to set the character '\0'

yourself in the returned array char

and reserve one extra byte of memory for that pointer). It also has undefined behavior (as Cool Guy pointed out) on its first iteration, since it is pom

always at least 1, which results in i - pom

, so it tries to access src[-1]

.

+1


source







All Articles