C 用已知的最大长度逐个字符构建字符串

标签 c string parsing

我正在尝试将字符一个接一个地添加到字符串中。我有这样的东西:

void doline(char *line, char *buffer, char** tokens){
}

我这样调用它:

char *line = malloc(1025 * sizeof(char *));
fgets(line, 1024, stdin);
int linelength = strlen(line);
if (line[linelength - 1] == '\n'){
    line[linelength - 1] = '\0';
}

char ** tokens = (char **) malloc(strlen(line) * sizeof(char *));
char *emptybuffer = malloc(strlen(line) * sizeof(char *));

parseline(line, emptybuffer, tokens);

因此 doline 将遍历 line 并根据各种条件对其进行标记,并将其片段放入 标记 中。我正在变量 buffer 中构建临时字符串。为此,我需要逐个字符地遍历line

我目前正在做的事情:

buffer[strlen(buffer)] = line[i];

然后在循环结束时:

*buffer++ = '\0';

但这就是结果:

printf("Working on line: '%s' %d\n", line, strlen(line));

输出:在线工作:'test'4

但是到函数结束时,缓冲区是:

*buffer++ = '\0';
printf("Buffer at the very end: '%s' %d\n", buffer, strlen(buffer));

输出:最后的缓冲区:'test'7

所以输出显示字符串变得困惑。逐个字符构建此字符串的最佳方法是什么?我的字符串操作正确吗?

任何帮助将不胜感激!

谢谢!

最佳答案

有一些基本问题,所以我重新编写了程序。

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

#define str_len 180

void tokenize(char *str, char **tokens)
{
    int length = 0, index = 0;
    int i = 0;
    int str_i;
    int tok_i;

    while(str[length]) {
        if (str[length] == ' ') {
            /* this charecter is a space, so skip it! */
            length++;
            index++;

            tokens[i] = malloc(sizeof(char) * index);

            tok_i = 0;           
            for (str_i=length-index ; str_i<length; str_i++) {
                tokens[i][tok_i] = str[str_i];
                tok_i++;
            }

            tokens[i][tok_i] = '\0';
            i++;
            index = 0;
        }
        length++;
        index++;
    }       

    /* copy the last word in the string */
    tokens[i] = malloc(sizeof(char) * index);
    tok_i = 0;           
    for (str_i=length-index ; str_i<length; str_i++) {
        tokens[i][tok_i] = str[str_i];
        tok_i++;
    }
    tokens[i][tok_i] = '\0';
    tokens[i++] = NULL;

    return;         
}

int main()
{
    char *str = malloc(str_len * sizeof(char));
    char **tokens = malloc(100 * sizeof(char *));
    int i = 0;

    if (str == NULL || tokens == NULL)
        return 1;

    gets(str);
    printf("input string: %s\n", str);
    tokenize(str, tokens);

    while(tokens[i] != NULL) {
        printf("%d - %s \n", i, tokens[i]);
        i++;
    }

    while(tokens[i])
        free(tokens[i]);
    free(tokens);
    free(str);

    return 0;
}

编译执行如下:

$ gcc -ggdb -Wall prog.c 
$ ./a.out 
this is a test string... hello world!! 
input string: this is a test string... hello world!! 
0 - this  
1 - is  
2 - a  
3 - test  
4 - string...  
5 - hello  
6 - world!!  
$ 

有一些基本假设:

  1. 传入字符串的长度假定为常量。这可以动态完成 - 请检查此 - How to read a line from the console in C? .

  2. 标记数组的长度也假定为常量。这也可以改变。我将把这个问题留给您来了解如何实现!

希望这有帮助!

关于C 用已知的最大长度逐个字符构建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308708/

相关文章:

c - 如何计算 ls 多列显示所需的列宽?

未终止字符串的 JavaScript 错误

java - 将数组元素从字符串转换为整数

java - 在 Java 中读取 CSV 文件时跳过第一行

algorithm - 具有优先级的方程(表达式)解析器?

python - scrapy.Request 不回调我的函数

c - 使用c无法打印复杂的数字模式

c - C 中的大位数组

c++ - 从 std::string 中删除所有 xml 标签

c++ - 从 Linux 中的 C/C++ 程序发送电子邮件