【问题标题】:C Splitting StringsC 拆分字符串
【发布时间】:2016-02-26 18:41:01
【问题描述】:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char *strArray[40];

void parsing(char *string){
  int i = 0;
  char *token = strtok(string, " ");
  while(token != NULL)
  {
    strcpy(strArray[i], token);
    printf("[%s]\n", token);
    token = strtok(NULL, " ");
    i++;
  }
}

int main(int argc, char const *argv[]) {
char *command = "This is my best day ever";
parsing(command); //SPLIT WITH " " put them in an array - etc array[0] = This , array[3] = best

return 0;
}

这是我的代码,有什么简单的方法可以解决吗?顺便说一句,我的代码不起作用。我是 C 语言编码的新手,我不知道如何处理它:( 帮助

【问题讨论】:

    标签: c string split strtok string-literals


    【解决方案1】:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *strArray[40];
    
    void parsing(const char *string){//Original does not change
        int i = 0;
        strArray[i++] = strdup(string);//make copy to strArray[0]
    
        char *token = strtok(*strArray, " ");
        while(token != NULL && i < 40 - 1){
            strArray[i++] = token;
            //printf("[%s]\n", token);
            token = strtok(NULL, " ");
        }
        strArray[i] = NULL;//Sentinel
    
    }
    
    int main(void){
        char *command = "This is my best day ever";
        parsing(command);
    
        int i = 1;
        while(strArray[i]){
            printf("%s\n", strArray[i++]);
        }
        free(strArray[0]);
        return 0;
    }
    

    int parsing(char *string){//can be changed
        int i = 0;
    
        char *token = strtok(string, " ");
        while(token != NULL && i < 40){
            strArray[i] = malloc(strlen(token)+1);//Ensure a memory for storing
            strcpy(strArray[i], token);
            token = strtok(NULL, " ");
            i++;
        }
        return i;//return number of elements
    }
    
    int main(void){
        char command[] = "This is my best day ever";
        int n = parsing(command);
    
        for(int i = 0; i < n; ++i){
            printf("%s\n", strArray[i]);
            free(strArray[i]);
        }
        return 0;
    }
    

    【讨论】:

    • 感谢您的关注,当我尝试访问该数组的第二个对象时,它显示为 NULL。例如,当我键入 printf(%s, strArray[1]) 时,它返回 null。为什么会这样?
    • @Berkin 详情未知。请注明可以复制的具体例子。
    • 这是我的代码。 github.com/berkin768/CSplit.git 。当我写 [1] 时,它返回 null。我不知道为什么。非常感谢:)
    • @Berkin scanf("%s",command); : %s 不接受空格。(所以command 有一个字串)所以改为scanf("%49[^\n]%*c", command);fgets(command, sizeof(command), stdin);...... = strtok( XXXXX, "\n");`
    • 是的,它有效,非常感谢我喜欢堆栈溢出
    【解决方案2】:

    strtok() 实际上修改了提供的参数,因此您不能传递字符串文字并期望它起作用。

    您需要有一个可修改的参数才能使其正常工作。

    根据man page

    使用这些功能时要小心。如果您确实使用它们,请注意:

    • 这些函数修改它们的第一个参数。

    • 这些函数不能用于常量字符串。

    FWIW,任何修改字符串文字的尝试都会调用undefined behavior

    【讨论】:

      【解决方案3】:

      在你们的帮助下我已经完成了,谢谢大家:)

      它是我的拆分库 = https://goo.gl/27Ex6O

      代码不是动态的,如果你输入 100 位输入,我猜它会崩溃

      我们可以使用带有这些参数的库:

      解析(输出文件、输入文件、分割字符)

      谢谢

      【讨论】:

        猜你喜欢
        • 2017-06-16
        • 2011-02-13
        • 2011-11-25
        • 2021-05-08
        • 1970-01-01
        • 2021-12-21
        相关资源
        最近更新 更多