c - 用空格分割字符串

标签 c c99

在阅读此问题之前,请注意我的问题与学校作业有关。对于这个赋值,我们唯一可以使用的函数是 malloc()。其他一切都必须在不使用其他库的情况下完成。

我正在调用函数 ft_split_whitespaces。此函数将字符串作为输入并将其“拆分”为单词。单词是分隔的空格、制表符和换行符。

#include <stdio.h>

char **ft_split_whitespaces(char *str);

int main(void)
{
    char *str = "what is";
    char **test = ft_split_whitespaces(str);

} 

关于上面的例子,每个索引的结果应该

test[0] = "what"
test[1] = "is"
test[2] = NULL

但是,我只能打印 test[0] 的结果。所有其他索引都不会打印出来显示。下面是一些代码示例,我认为应该打印我的函数的结果。

int i = 0;
while(test[i] != NULL)
{
   printf("%s", test[i]);
   i++;
}

当这部分代码运行时,只有 test[0] 被打印到输出中。我一直坐在这里试图调试我的代码几个小时。如果有人有空闲时间并且不介意查看我的代码,我将不胜感激。我感觉这可能与我使用 malloc 的方式有关,但我仍然无法弄清楚。

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


int     count_whitespaces(char *str)
{
    int space_count;
    int i;

    space_count = 0;
    i = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ' ' || str[i] == 9 || str[i] == '\n')
        {
            if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
                space_count++;
        }
        i++;
    }
    return (space_count);
}

int     check_whitespace(char c)
{
    if (c == ' ' || c == 9 || c == '\n' || c == '\0')
    {
        return (1);
    }
    else
        return(0);
}

int     count_characters(char *str, int i)
{
    int char_count;

    char_count = 0;
    while(str[i])
    {
        if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
            char_count++;
        else
            break;
        i++;
    }
    return (char_count);
}

char    **ft_split_whitespaces(char *str)
{
    int     i;
    int     j;
    int     k;
    char    **word;
    int     space;

    i = 0;
    j = 0;
    k = 0;
    word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
    while(str[i] != '\0')
    {
        if (check_whitespace(str[i]) == 1)
            i++;
        else
        {           
            if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
                return (NULL);
            while (check_whitespace(str[i]) == 0)
            {
                word[j][k] = str[i];
                i++;
                k++;
            }
            j++;
        }       
    }
    j++;
    word[j] = NULL;
    j = 0
    return word;
}

最佳答案

您忘记重置kft_split_whitespaces 中的外层 while 循环应该是这样的

  while (str[i] != '\0') {
    if (check_whitespace(str[i]) == 1){
      i++;
    }
    else {
      if ((word[j] =
           malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL){
        return (NULL);
      }
      while (check_whitespace(str[i]) == 0) {
        word[j][k] = str[i];
        i++;
        k++;
      }
      word[j][k] = '\0';
      j++;
      k = 0;             // reset k
    }
  }

关于c - 用空格分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39060343/

相关文章:

c - "Chances"函数发生吗?

c - "int"真的需要至少与 C 中的 "short"一样大吗?

c - C99 有#define 吗?

c - 为什么两个 .C 文件中的同名私有(private)函数会导致多重定义错误?

c - 在按Ctrl + D后停止用户输入需要按2次。为什么?

C 将 float 转换为整数

来自字符串快捷方式的 C 字符

c++ - 无法在 Visual Studio 2012 中打开头文件

c - 点 (.) 在结构初始值设定项中是什么意思?

c - 用户输入以创建链接列表