在字符串操作函数中,除了经常用的连接、拷贝、求长度等,今天我们来说说可以在一个字符串里查找另外一个字符串的函数:strstr和strpbrk函数。定义如下:
#include <string.h>
char *strstr(const char *haystack, const char *needle);
char *strpbrk(const char *s, const char *accept);
二者在调用成功都会返回查找的字符串位置,失败则返回NULL。
strstr是在haystack中查找needle字符串所在的位置,strpbrk则是在s中查找accept中任意第一个字符出现的位置。
代码实现:
执行结果: