该库函数包含在 < string.h > 头文件中, 函数原型: extern char *strstr(char *str1, const char *str2); 使用方法 char *strstr(char *str1, char *str2); 意义为 判断 str2 是否为 str1 的字串, 若是则返回 str2 在 str1 中首次出现的指针位置, 若不是返回 NULL;
类似的 strchr(str,ch).strchr 函数原型: char * strchr(char * str,char ch); 功能就是找出在字符串 str 中第一次出项字符 ch 的位置, 找到就返回该字符位置的指针 (也就是返回该字符在字符串中的地址的位置), 找不到就返回空指针 (就是 null).
strstr 举例
- #include<stdio.h>
- #include<string.h>
- void main(){
- char *str1 = "WHEN YOU HERE PEOPLE SING";
- char *str2 = "PEOPLE";
- char *pl;
- pl=strstr(str1,str2);
- printf("Substring is %s\n",pl);
- }
运行结果:
----------------
来源: http://www.bubuko.com/infodetail-3329739.html