strtok函数

调用方式:char *strtok(char *str1,char *str2);   
功能说明:函数strtok()返回字符串str1中指向一个由str2所指定的字符或者字符串的分隔符的指针,当没有要返回的分隔符时,就返回一个空指针。  
   
   函数strtok()实际上修改了有str1指向的字符串。每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,函数用这种方法来连续查找该字符串。  
   
  例子:  
  #include <string.h>  
  #include <stdio.h>  
   
  int main( int argc, char *argv[] )  
  {   
     char *p;   
     char str[100]="This is a test,and you can use it";   
     p = strtok(str," "); //注意,此时得到的   p为指向字符串:"This",即在第一个分隔符前面的字符串,即每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,所以此时NULL指针指向后面的字符串:"is   a   test   ,and   you   can   use   it"。   
            
     printf("%s\n",p); //此时显示:This   
     do   
     {   
        p = strtok(NULL, ","); //NULL即为上面返回的指针,即字符串:"is a test ,and you can use it"。   
        if(p)   
           printf("|%s",p);   
     }while(p);    
          
     return 0;  
  }    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章