C语言 strstr()函数详解加应用
1.头文件
#include<string.h>
2.描述
C 库函数 char *strstr(const char *haystack, const char *needle) 在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 '\0'。
3.声明
char *strstr(const char *haystack, const char *needle)
4.参数
haystack -- 要被检索的 C 字符串。
needle -- 在 haystack 字符串内要搜索的小字符串。
5.返回值
该函数返回在 haystack 中第一次出现 needle 字符串的位置,如果未找到则返回 null。
6.示例
#include <stdio.h>
#include <string.h>int main(int argc,char *argv[])
{char *str1="abcdefghijk";char *str2="h";char *p;p=strstr(str1,str2);printf("匹配成功的字符串:%s\n",p);printf("字符串的位置:%d\n",p-str1+1);return 1;
}
运行程序