- 论坛徽章:
- 0
|
不使用已有的系统调用或函数库,实现c库的函数:strstr()
原帖由 "mq110" 发表:
还有 你的程序不符合 strstr的函数原形
char *strstr(const char *haystack, const char *needle);
既然能用while 改. 我想你也不必再用goto.
当时感觉用while(1)循环, 可能会每次循环做一次判断.
而且,用goto感觉好点,简洁些.是根据算法而想的.
如果原型改为 char *(const char* s, const char *d); 就更容易实现了.
char * __strstr(const char *s, const char *d)
{
int ret = 0;
int i = 0, j = 0;
loop:
if (*(d+i) == *(s+j)) {
i++; j++;
if (*(d+i) == '\0')
return s + ret;
goto loop;
} else {
i = 0;
ret++; j = ret;
if (*(s+j) == '\0')
return 0;
goto loop;
}
} |
|