字符串函数strstr(char *s1,char *s2)是返回字符串s1中首次出现s2的地址 函数实现:
[cpp] view plain copy
- char *my_strstr( char *s1, char *s2)
- {
- if(*s2 == '\0')
- {
- return NULL;
- }
-
- char *a = s1;
- char *b = s1;
- char *c = s2;
-
- while(*a != '\0')
- {
- if(*b == *c)
- {
- b++;
- c++;
- if(*c == '\0')
- {
- return a;
- }
- }
- else
- {
- a++;
- b = a;
- c = s2;
- }
- }
- return NULL;
- }
验证:
[cpp] view plain copy
- int main()
- {
- char *src = NULL;
- src = my_strstr("abcdefg","cde");
- printf("src = %s\n",src);
- return 0;
- }
结果:
|