繁体   English   中英

将行数组拆分为单词

[英]Splitting a line array into words

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

    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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM