新浪微博的@文艺复兴记 贴了个面试题,难住了一位arm Linux 10年经验的人(上海年薪36w),本来觉得做库函数实现又难又琐碎还没成就感,但这个貌似比较简单,于是试做了下,20min写加调试通过(codeblocks),献丑了。
#include <stdio.h>
#include <stdlib.h>
int isSubStr(char *target, char *source)
{
char *pBase = source;
int cnt;
while (*pBase)//not end of str
{
cnt=0;//clear tmp inner cursor
while (*(target+cnt) && *(pBase+cnt))//not end of both str
{
if (*(target+cnt) != *(pBase+cnt))
break;//do not use continue
cnt++;
}
if ('\0' == *(target+cnt))//bingo!
return 0;
pBase++;
}
return 1;
}
int main()
{
int res = isSubStr("eeewh", "lwhplps");
printf("res = %d\n", res);
return 0;
}
本文分享了一个简单的子串查找函数实现,该函数用于判断一个字符串是否为另一个字符串的子串。作者在20分钟内完成了从编写到调试的全过程,并通过示例验证了其正确性。

1万+

被折叠的 条评论
为什么被折叠?



