可以使用strtok函數做分割單詞。
#include<string.h>
voidmain()
{
chars[]="192.168.0.26";
char*delim=".";
char*p;
printf("%s",strtok(s,delim));
while((p=strtok(NULL,delim)))
printf("%s",p);
printf("\n");
}
擴展資料
在C++中strtok的使用
#include<iostream>
#include<cstring>
usingnamespacestd;
intmain()
{
charsentence[]="Thisisasentencewith7tokens";
cout<<"Thestringtobetokenizedis:\n"<<sentence<<"\n\nThetokensare:\n\n";
char*tokenPtr=strtok(sentence,"");
while(tokenPtr!=NULL){
cout<<tokenPtr<<endl;
tokenPtr=strtok(NULL,"");
}
//cout<<"Afterstrtok,sentence="<<tokenPtr<<endl;
return0;
}