首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

分享字符串查寻函数 求指导

2012-10-17 
分享字符串查找函数求指导char* find(char* data_buf, char* pattern){char* buf_cur data_bufchar* pt

分享字符串查找函数 求指导
char* find(char* data_buf, char* pattern)
{
char* buf_cur = data_buf;
char* pt_cur = pattern;

char* last_find_str = NULL;
int last_find = 0;

if (NULL == data_buf
|| NULL == pattern)
{
return NULL;
}

while (*buf_cur != 0 && *pt_cur != 0)
{
if (*buf_cur == *pt_cur)
{
if (0 == last_find)
{
last_find_str = buf_cur;
}

buf_cur++;
pt_cur++;

last_find = 1;
}
else
{
if (last_find)
{
buf_cur = last_find_str + 1;
pt_cur = pattern;
}
else
{
buf_cur++;
}

last_find = 0;
}
}

if (0 == *pt_cur)
{
return last_find_str;
}

return NULL;
}

[解决办法]

C/C++ code
char * __cdecl cc_strstr(        const char *str1,        const char *str2        ){        char *str1Cp = (char *)str1;        char *s1, *s2;        if(!*str2)                return (char *)str1;        while(*str1Cp)        {                s1 = str1Cp;                s2 = (char *)str2;                while(*s1 && *s2 && !(*s1 - *s2))                        ++s1, ++s2;                if(!*s2)                        return str1Cp;                ++str1Cp;        }        return NULL;} 

热点排行