69,373
社区成员
发帖
与我相关
我的任务
分享
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;
}
#include <stdio.h>
#include <string.h>
char* find(char* data_buf, char* pattern);
int main()
{
char s[] = " char* find(char* data_buf, char* pattern)";
printf("%s\n", find(s , "find"));
printf("%s\n", strstr(s , "find"));
return 0;
}
// char *strstr( const char *str1, const char *str2 );
// 功能:函数返回一个指针,它指向字符串str2 首次出现于
// 字符串str1中的位置,如果没有找到,返回NULL。
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;
}