如何在DECOM C $焦炭的对$ PS阵列 [英] How to decompres array of char in c

查看:95
本文介绍了如何在DECOM C $焦炭的对$ PS阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建功能:

 的char * DECOM preSS(为const char * SRC){
}

输入这个功能是一些字符串:

 世界,你好! - >你好,世界!Hel2o世界10 - >您好!世界!!!!!!!!!!

你可以看到,如果有一定数量它重复$​​ P $ pvious字符这个号码倍。我是Java程序员,但现在我需要用C来解决这个;

我现在这一点。它只是打印我希望正确的价值,但我不知道如何分配它返回指针

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&ASSERT.H GT;字符* DECOM preSS(为const char * SRC){
    INT最大= 0;
    INT POM = 1;    字符* vysledek =;
    INT I;
    对于(i = 0; I<的strlen(SRC);我++){
        最大= 0;
        聚甲醛= 1;        而(ISDIGIT(SRC [i])){
            INT位=(SRC [I] - '0');
            最大值=最大值* 10 +数字;
            我++;
            POM ++;
        }        如果(最大== 0){
            最大= 1;
        }        诠释J;
        为(J = 0; J<最大; J ++){
            的printf(%C,SRC [我 - POM]);
        }
    }    返回vysledek;
}INT主(INT ARGC,CHAR *的argv []){    DECOM preSS(Hel2o世界10!);    返回0;
}


解决方案
既然你提到,你是一个Java程序员

,你应该看看为C内存分配(在这种情况下,尤其是字符串)是如何工作的:

 的char * vysledek =;

在Java中,这将创建一个字符串对象,在那里你可以简单地在你的意志添加字符。在C,然而,这基本上会创建的char [1] ,载'\\ 0'(字符串数组终止字符)。

自 - 像Java的数组 - 你不能(也不应该)写一个数组的边界之外,你可以存储这个数组中的一个字符,这是不足够的内存,为你的函数

您可以找出你将有多少内存通过循环输入第一个分配(如尼特在他的评论中提到)。然后你可以的malloc 为输出指针必要的内存:

  vysledek =的malloc(小于输出字符串长度GT;);


现在,您已经分配足够的内存,你可以写你的输出到这个数组,所以不是

 的printf(%C,SRC [我 -  POM]);

您现在可以这样写:

  vysledek [vysledek_counter ++] = SRC [我 - 聚甲醛]

和返回指针。



请注意,您的的释放返回的值,你用它做之后,即使是你的主要方法结束,这应该是这个样子:

  INT主(INT ARGC,CHAR *的argv []){
    字符*输出;    输出= DECOM preSS(Hel2o世界10!);
    的printf(%S \\ n,输出);
    免费(输出);    返回0;
}

另外也请注意补充:如果遇到非结尾的字符串(也就是你的code会失败,没有'\\ 0'字符表示字符串的结尾)。为了解决这个问题,通过一个额外的字符串长度参数(你必须设置'\\ 0'人物自己在返回字符阵列,并保留该指针的内存的一个附加字节)。它也有不确定的行为在其第一次迭代(如很酷的家伙所指出的),如 POM 总是至少为1,因此 I - POM ,因此试图访问 SRC [-1]

I need to create function:

char * decompress(const char * src) {
}

input to this function is some string:

Hello world! -> Hello world!

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

as you can see if there is some number it repeat previous char this number times. I am java programmer but now I need to solve this in c;

I have now this. It just print I hope right value but I dont know how to assign it to return 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;
}

解决方案

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

char *vysledek = "";

In Java, this would create a string object, where you can simply add characters at your will. In C, however, this will basically create an array of char[1], containing '\0' (string termination character).

Since - like Java's arrays - you can't (shouldn't) write outside of an array's boundaries, you could store one character in this array, which is of not sufficient memory for your function.

You can find out how much memory you will have to allocate by iterating over the input first (as Nit has mentioned in his comment). Then you can malloc the necessary memory for your output pointer:

vysledek = malloc (<output string length>);


Now that you have allocated enough memory, you can write your output into this array, so instead of

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

you can now write:

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

and return the pointer.


Please note that you should free the returned value after you're done using it, even if it's at the end of your 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 additional note to add: Your code will fail if it encounters a non-terminated string (that is, there is no '\0' character to indicate the string's end). To counter this, pass an additional string-length parameter (you'll have to set the '\0' character yourself in the returned char array, and reserve one additional byte of memory for this pointer). It also has undefined behaviour (as Cool Guy has pointed out) on its first iteration, as pom is always at least 1, resulting in i - pom, therefore trying to access src[-1].

这篇关于如何在DECOM C $焦炭的对$ PS阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆