//编程实现字符串中子串的查找
const char* findStr(const char* src,const char* sub)
{
const char* bp;
const char* sp;
if (src == NULL || sub == NULL) return NULL;
while (*src != '\0') //遍历src字符串
{
bp = src; //用于src的遍历
sp = sub; //用于sub的遍历
do
{
if (!*sp) //如果到了sub字符串结束符位置
return src; //表示找到了sub字符串,退出
} while (*bp++ == *sp++);
src += 1;
}
return NULL;
}
int main()
{
char strSrc[] = "Hello world!";
char ch[] = "wor";
const char* s = findStr(strSrc, ch); //world
printf("字符串为: %s\n", s);
system("pause");
return 0;
}
C编程实现字符串中子串的查找
最新推荐文章于 2024-05-25 12:57:47 发布