C-在空白处拆分字符串

C-在空白处拆分字符串,c,split,C,Split,我需要在有空格的地方拆分一个字符串(例如字符串:Hello这是一个示例字符串。将其拆分为一个单词数组。我不确定这里缺少了什么,我也想知道测试此函数的最佳方法是什么。唯一允许的库函数是malloc 感谢您的帮助 #include <stdlib.h> char **ft_split(char *str) { int wordlength; int wordcount; char **wordbank; int i; int current;

我需要在有空格的地方拆分一个字符串(例如字符串:
Hello这是一个示例字符串。
将其拆分为一个单词数组。我不确定这里缺少了什么,我也想知道测试此函数的最佳方法是什么。唯一允许的库函数是
malloc

感谢您的帮助

#include <stdlib.h>

char **ft_split(char *str) {
    int wordlength;
    int wordcount;
    char **wordbank;
    int i;
    int current;

    current = 0;
    wordlength = 0;
    //while sentence
    while (str[wordlength] != '\0') {
        //go till letters
        while (str[current] == ' ')
            current++;
        //go till spaces
        wordlength = 0;
        while (str[wordlength] != ' ' && str[wordlength] != '\0')
            wordlength++;
         //make memory for word
        wordbank[wordcount] = malloc(sizeof(char) * (wordlength - current + 1));

        i = 0;
        //fill wordbank current
        while (i < wordlength - current) {
            wordbank[wordcount][i] = str[current];
            i++;
            current++;
        }

        //end word with '\0'
        wordbank[wordcount][i] = '\0';

        wordcount++;
    }
    return wordbank;
}
#包括
字符**ft_分割(字符*str){
int字长;
int字数;
字符库;
int i;
电流;
电流=0;
字长=0;
//服刑期间
while(str[wordlength]!='\0'){
//收信
while(str[当前]='')
电流++;
//退房
字长=0;
while(str[wordlength]!='&&str[wordlength]!='\0')
字长++;
//记忆单词
wordbank[wordcount]=malloc(sizeof(char)*(wordlength-current+1));
i=0;
//填充字库电流
while(i
您也需要
malloc()
wordbank。您可以计算单词的数量,然后

wordbank = malloc((count + 1) * sizeof(*wordbank));
if (wordbank == NULL)
    return NULL;

注意
sizeof(char)
定义为1。而
sizeof*指针始终是您想要的。

您的代码中存在多个问题:

  • 您没有为要指向的
    wordbank
    分配数组,取消引用未初始化的指针具有未定义的行为
  • 扫描字符串的方法已中断:在循环中重置
    wordlength
    ,以便从字符串的开始处继续重新扫描
  • 您应该在数组中为尾部空指针分配一个额外的条目,以向调用者指示数组的结尾
以下是修改后的版本:

#include <stdlib.h>

char **ft_split(const char *str) {
    size_t i, j, k, wordcount;
    char **wordbank;

    // count the number of words:
    wordcount = 0; 
    for (i = 0; str[i]; i++) {
        if (str[i] != ' ' && (i == 0 || str[i - 1] == ' ')) {
            wordcount++;
        }
    }

    // allocate the word array
    wordbank = malloc((wordcount + 1) * sizeof(*wordbank));
    if (wordbank) {
        for (i = k = 0;;) {
            // skip spaces
            while (str[i] == ' ')
                i++;
            // check for end of string
            if (str[i] == '\0')
                break;
            // scan for end of word
            for (j = i++; str[i] != '\0' && str[i] != ' '; i++)
                continue;
            // allocate space for word copy
            wordbank[k] = p = malloc(i - j + 1);
            if (p == NULL) {
                // allocation failed: free and return NULL
                while (k-- > 0) {
                    free(wordbank[k]);
                }
                free(wordbank);
                return NULL;
            }
            // copy string contents
            memcpy(p, str + j, i - j);
            p[i - j] = '\0';
        }
        // set a null pointer at the end of the array
        wordbank[k] = NULL;
    }
    return wordbank;
}
#包括
字符**ft\u拆分(常量字符*str){
大小i,j,k,字数;
字符库;
//计算字数:
字数=0;
对于(i=0;str[i];i++){
if(str[i]!=''&(i==0 | | str[i-1]=''){
字数++;
}
}
//分配单词数组
wordbank=malloc((wordcount+1)*sizeof(*wordbank));
如果(wordbank){
对于(i=k=0;;){
//跳过空格
while(str[i]=='')
i++;
//检查字符串的结尾
如果(str[i]='\0')
打破
//扫描词尾
对于(j=i++;str[i]!='\0'和&str[i]!='';i++)
继续;
//为word副本分配空间
字库[k]=p=malloc(i-j+1);
if(p==NULL){
//分配失败:释放并返回NULL
而(k-->0){
免费(wordbank[k]);
}
免费(wordbank);
返回NULL;
}
//复制字符串内容
memcpy(p,str+j,i-j);
p[i-j]='\0';
}
//在数组末尾设置空指针
wordbank[k]=NULL;
}
返回字库;
}

为什么不使用
strchor()
?或
strtok_s
/
strtok_r
?您目前正在使用“piscine”吗?除非您打算只在英语中使用它,否则您可能想查看ICU(Unicode的国际组件)。其他语言可以有多字节字符(如果使用标准UTF-8编码)而且单词之间可能没有空格,这使得情况非常复杂。使用ICU库,您可以轻松获得单词开头和结尾的指针,而不必担心血淋淋的细节。是的,这是
sizeof(*pointer)
这个技巧看起来很酷,我也见过有人像你一样向别人推荐它,但人们怎么能取消对未初始化指针的引用呢?据我所知,这是未定义的行为…@ForceBru
sizeof
在不需要参数时不计算参数。请看@BLUEPIXY,这确实很聪明。谢谢!
wordbank
应分配给一个以上的字数,并应在最后一个位置存储一个空指针,以便调用者可以从数组内容中确定字数。