strstr和strtok函数

来源:百度文库 编辑:神马文学网 时间:2024/04/29 23:31:16

 char *strtok(char *s, char *delim);
 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
 说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。strtok在s中查找包含在delim中的字符并用'\0'来替换,直到找遍
                整个字符串。
 返回值:从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处
                分割的节点。

#include 
#include 
int main(void)
{

    char buf1[]="wo shi yi ge xuesheng";
    char * token = strtok(buf1, " ");
    while( token != NULL )
    {
        printf( "%s_\n", token);
        token = strtok( NULL, " ");
    }
    return 0;
}

output:
wo_
shi_
yi_
ge_
xuesheng_
Press any key to continue  
char *strstr (const char *s1, const char *s2);
说明:返回一个指针,它指向s1中第一次出现s2字符序列(不包括'\0'字符)的位置,没有匹配的则返回NULL
#include 
#include  
 
int main(void)
{
    char *haystack="aaa||a||bbb||c||ee||";
    char *needle="||";
    char *buf = strstr( haystack, needle);
    if(buf != NULL)
        printf( "%s\n", buf);
    return 0;
}
output:
||a||bbb||c||ee||
Press any key to continue