【问题标题】:Adding strings to a char array and separating by \0 in C将字符串添加到 char 数组并在 C 中用 \0 分隔
【发布时间】:2017-02-12 22:01:07
【问题描述】:

我正在尝试编写一些代码,不断将单词添加到一个名为 sTable 的字符数组中,并用 \0 分隔。 sTable 的最终内容类似于:

"单词\0int\0paper\0mushroom\0etc\0"

我的第一个想法是将单词读入一个单独的字符数组 tempWord,然后将它们连接在一起,但是,我怎样才能在它们之间添加 \0 并保持 sTable ass 最终的数组?我对C不是很熟悉,提前感谢您的帮助!

【问题讨论】:

  • 你可以在单个字符数组中很容易地做到这一点,它不会是一个普通的“C 字符串”。也许你需要描述你在做什么以及为什么?
  • @John3136 我正在尝试将从输入中读取的单词存储在单个字符数组 sTable 中(我实际上是在使用 Lex 制作扫描仪,所以这些单词是将成为代币)。正如我所说的,我想将它们添加到由 \0 分隔的 sTable 中。另外,正如我在单词中添加的那样,我试图将它们的第一个字母的索引存储在另一个变量(yylval)中。
  • 当字符被添加到 sTable[] 时,您打算如何跟踪使用了多少 sTable[]
  • @chux 这实际上是我遇到的问题之一。我想我必须动态分配 sTable(我知道如何在 C++ 中但不知道如何在 C 中做,所以我必须研究)并有一个 max_capacity 和一个 current_size 变量来跟踪。

标签: c arrays char concatenation


【解决方案1】:

您可以通过为单词数组中应该接收单词的下一个位置保留一个指针来做到这一点。下面的函数add_word() 接受一个指向chars 数组中的位置的指针和一个字符串。将wrd 添加到以next 开头的位置后,将返回指向空终止符后的位置的指针。 next 指针最初被赋予字符数组words[] 中第一个位置的地址。请记住,这里没有错误检查,因此调用者负责确保字符串适合数组。

#include <stdio.h>

#define MAX_SIZE  1000

char * add_word(char *next, const char *wrd);

int main(void)
{
    char words[MAX_SIZE];
    char *next = words;

    next = add_word(next, "word");
    next = add_word(next, "int");
    next = add_word(next, "mushroom");
    next = add_word(next, "etc");

    for (char *ptr = words; ptr < next; ptr++) {
        if (*ptr == '\0') {
            printf("\\0");
        } else {
            putchar(*ptr);
        }
    }

    putchar('\n');

    return 0;
}

char * add_word(char *next, const char *wrd)
{
    while (*wrd != '\0') {
        *next++ = *wrd++;
    }
    *next++ = '\0';

    return next;
}

程序输出:

word\0int\0mushroom\0etc\0

这里是上述程序的一个版本,经过修改,add_word() 函数获取要添加的单词的起始位置的索引,并返回下一个单词的索引。还添加了一个数组 word_indices[] 以保存添加到 words[] 的每个单词的起始索引。

#include <stdio.h>

#define MAX_SIZE  1000

size_t add_word(char *tokens, size_t next, const char *wrd);

int main(void)
{
    char words[MAX_SIZE];
    size_t word_indices[MAX_SIZE] = { 0 };
    size_t  next = 0, count = 0;

    char *input[4] = { "word", "int", "mushroom", "etc" };

    for (size_t i = 0; i < 4; i++) {
        next = add_word(words, next, input[i]);
        word_indices[++count] = next;
    }

    /* Show characters in words[] */
    for (size_t i = 0; i < next; i++) {
        if (words[i] == '\0') {
            printf("\\0");
        } else {
            putchar(words[i]);
        }
    }
    putchar('\n');

    /* Print words in words[] */
    for (size_t i = 0; i < count; i++) {
        puts(&words[word_indices[i]]);
    }

    return 0;
}

size_t add_word(char *tokens, size_t next, const char *wrd)
{
    while (*wrd != '\0') {
        tokens[next++] = *wrd++;
    }
    tokens[next++] = '\0';

    return next;
}

程序输出:

word\0int\0mushroom\0etc\0
word
int
mushroom
etc

【讨论】:

  • @AlessandroLorusso--因为你在 cmets 中提到你的原始问题你有兴趣保存存储单词的索引,所以我添加了一个使用索引并将索引保​​存到我的答案的版本。
  • 谢谢您,您的回答最终帮助我理解了这一点并完成了我的程序。
猜你喜欢
  • 2011-04-03
  • 1970-01-01
  • 2022-01-10
  • 2022-09-23
  • 2013-10-09
  • 2021-03-18
  • 2014-12-20
  • 2021-05-23
  • 1970-01-01
相关资源
最近更新 更多