gpt4 book ai didi

c - 将线阵列拆分为单词

转载 作者:太空宇宙 更新时间:2023-11-04 04:41:33 25 4
gpt4 key购买 nike

我有一个由一行单词组成的数组(我使用了 fgets)。我现在想创建一个新数组作为包含相同行的同一结构的一部分,但分成只有字母数字字符的单词(这样 line[1].word_in[3] 会给我第 1 行的第 3 个单词).我做了一些尝试,但没有奏效。

    typedef struct {
char linewords[101];
char separateword[101];
} line;

line linenum[101];

while fgets(linenum[i].linewords, 101, stdin) != NULL) {
i++
linenum[i].separateword = linenum[i].linewords.Split(' ');
if (isalnum(int linenum[i].separateword) != 0) {
/* need to remove that character */
}
}

我很抱歉我真的不知道我在做什么。我的第一个问题显然是拆分结构,因为这给了我一个错误。谢谢

编辑:我已经添加了我现在正在做的事情;但是我在分配 var 时收到分配错误。

    typedef struct {
char linewords[101];
char separateword[101];
} line;

line linenum[101];
char var[101]
char *strtok(char *str, const char delim);

while fgets(linenum[i].linewords, 101, stdin) != NULL) {

char* strcopy();
char* strtok();
strcpy(linenum[i].separateword,linenum[i].linewords);
strtok(linenum[i].separateword, ' '); /*this line causes seg fault*/
i++
}
}

最佳答案

strtok() 返回一个字符指针。它在

char *strtok(char *s, const char *delim) ;

所以问题是你没有保存返回值。

while fgets(linenum[i].linewords, 101, stdin) != NULL) {

//strcpy(linenum[i].separateword,linenum[i].linewords);
linenum[i].seperatewords=strtok(linenum[i].linewords, ' ');
i++
}

它会解决你的问题。有关 strtok() 的更多详细信息: strtok()

关于c - 将线阵列拆分为单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25955632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com