strstok詳解

extern char *strtok(char *s, char *delim);

用法:#include <string.h> 功能:分解字符串爲一組標記串。s爲要分解的字符串,delim爲分隔符字符串。 說明:首次調用時,s必須指向要分解的字符串,隨後調用要把s設成NULL。 strtok在s中查找包含在delim中的字符並用NULL('\0')來替換,直到找遍整個字符串。 返回指向下一個標記串。當沒有標記串時則返回空字符NULL。

舉例:

#include<string.h>
#include<stdlib.h>

char  x[20001];
int main()
{
       int i,j,k,l;
       char *p,*q;
         while(gets(x))
         {
             if(strcmp(x,"#")==0) break;
             l=0;
             p=x;
             q=strtok(p," ");
             while(q)
             {
                    printf("%s\n",q);
                    q=strtok(NULL," ");
             }

               }
return 0;
}

分析輸入字符串,輸出以空格分隔的單詞。輸入以#結束

you are my friend

#

輸出爲:

you

are

my


本文來源於網絡小築 http://www.iwebtrados.com.cn/ , 原文地址:http://www.iwebtrados.com.cn/post/272.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章