我有以下问题:
// 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/