c语言实现简单分词

2021-03-21  本文已影响0人  一路向后

1.源码实现

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

char vocabulary[1024][32];

/*将分类的文本分割成单词*/
int SpliteToWord(char *text)
{
    char seps[] = " ,.!?\n";
    char *substring;
    int i = 0;

    substring = strtok(text, seps);
    while(substring != NULL)
    {
        strcpy(vocabulary[i], substring);
        substring = strtok(NULL, seps);
        i++;
    }

    return i;
}

int main(int argc, char **argv)
{
    char buf[4096];
    int n = 0;
    int i;

    if(argc < 2)
        return 0;

    memset(buf, 0x00, sizeof(buf));

    strncpy(buf, argv[1], 4095);

    n = SpliteToWord(buf);

    for(i=0; i<n; i++)
    {
        printf("%s\n", vocabulary[i]);
    }

    return 0;
}

2.编译源码

$ gcc -o example example.c

3.运行及其结果

$ ./example "I have a dream"
I
have
a
dream
上一篇 下一篇

猜你喜欢

热点阅读