1 #include <stdio.h>
2 #include <strings.h>
3 /*******************
4 * 头文件:#include <strings.h>
5 * 函数:char *index(const char *s, int c);
6 * 功能:从已有字符串中查找一个字符所在当前字符串的位置
7 * 参数:
8 * s:原字符串
9 * c:要查找的字符
10 * 返回值:
11 * 成功返回:非NULL的地址(在查找的字符串中找到字符的地址(索引))
12 * 失败返回:NULL
13 * **********************************************************/
14 int main()
15 {
16 char buf[120] = "abcdef sdcbdwd asdbfnwe";
17 char *p = NULL;
18 p = index(buf,'c');
19 if(p == NULL)
20 {
21 printf("查找值不存在\n");
22 }
23 else
24 {
25 printf("p = %s\n",p);
26 }
27
28 p = index(p+1,'c');
29 if(p == NULL)
30 {
31 printf("查找值不存在\n");
32 }
33 else
34 {
35 printf("p = %s\n",p);
36 }
37
38 return 0;
39 }
1 #include <string.h>
2 #include <stdio.h>
3 /*******************
4 * 头文件:#include <string.h>
5 * 函数:char *strstr(const char *haystack, const char *needle);
6 * 功能:从已有字符串中查找一个字符串所在当前字符串的位置
7 * 参数:
8 * haystack:原字符串
9 * needle:要查找的字符串
10 * 返回值:
11 * 成功返回:非NULL的地址(在查找的字符串中找到字符串的地址(索引))
12 * 失败返回:NULL
13 * **********************************************************/
14 int main()
15 {
16 char buf[1024] = "123456789012345678901234567890";
17 char *p = NULL;
18 p = strstr(buf, "5678");
19 if (p == NULL)
20 {
21 printf("查找值不存在\n");
22 }
23 else
24 {
25 printf("p = %s\n", p);
26 }
27
28 while (p != NULL)
29 {
30 p = strstr(p+1, "5678");
31 if (p == NULL)
32 {
33 printf("查找值不存在\n");
34 }
35 else
36 {
37 printf("p = %s\n", p);
38 }
39 }
40
41 return 0;
42 }