#include<stdio.h>
#include<string.h>
int main(){
char str[] = "This is a sample string";
char *pch;
char piece1[10] = " ";
char piece2[10] = " ";
printf("\n %s \n",str);
pch = strtok(str," ");
strcpy(piece1,pch);
printf("\n piece1 : %s \n",piece1);
while(pch != NULL){
printf("\n %s \n",pch);
pch = strtok(NULL," ");
if(pch != NULL){
strcpy(piece2,pch);
printf("\n piece2 : %s \n",piece2);
}
}
printf("\n str: %s \n",str); //prints only "This" and not the original string
return 0;
}
我无法理解strtok
以 NULL 作为第一个参数调用的第二个。当strtok
使用 str 调用时,它会拆分第一个单词。strtok(NULL," ")
, 在NULL上做什么?
当我打印 str 时,它会打印第一个单词,而不是它在标记化之前包含的原始字符串。