gpt4 book ai didi

c - 将字符串拆分为单词

转载 作者:行者123 更新时间:2023-11-30 21:46:04 25 4
gpt4 key购买 nike

我有以下问题:

// Basically I am reading from a file and storing in local array.

char myText[100] = "This is text of movie Jurassic Park";

// here I want to store each work in to dictionary

st.insert(&myText[0]); // should insert "This" not till end of sentence.

// similarly for next word "is", "text" and so on.

如何用 C 语言做到这一点?

最佳答案

为此,您可以使用 strtok function :

char myText[100] = "This is text of movie Jurassic Park";
char *p;
for (p = strtok(myText," "); p != NULL; p = strtok(NULL," ")) {
st.insert(p);
}

请注意,此函数通过在分隔符所在的位置添加 NUL 字节来修改它正在解析的字符串。

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

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