strlen可以自己实现

DO IT YOURSELF !
程序代码:#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#define max 50000000
int is_substr(char* str,const char* substr)
{
int i=0,j;
int s_len=0;
int sub_len=0;
bool flag=true; //标记子串是否在源串中
char* p_s=str; //临时指针
const char* p_sub=substr; //临时指针
while(*p_s!=NULL)
{
s_len++;
p_s++;
} //计算源串长度
while(*p_sub!=NULL)
{
sub_len++;
p_sub++;
} //计算子串长度
p_sub=substr;
p_s=str;
if(sub_len>=s_len) return 0; //如果子串长度大于等于源串长度 直接返回
while(i<s_len-sub_len+1) //更正原来的一个bug增加一个(长度+1)
{
for(j=0;j<sub_len;j++) //这个循环用于判断子串是否在源串之间被包含
{
if(*p_s!=*p_sub)
{
flag=false;
break;
}
p_s++;
p_sub++;
}
if (flag==true) //找到字串直接返回
{
return 1;
}
i++; //下面几句也是用于用于判断子串是否在源串之间被包含
if(i!=s_len-sub_len+1) flag=true; //更正原来的一个bug增加一个(长度+1)
p_sub=substr;
p_s=++str; //一个一个的移动源串指针 用于用于判断子串是否在源串之间被包含
}
return 0;
}
int main()
{
srand((unsigned)time(NULL));
clock_t start,finish;
start=clock();
char* dest;
char tmp;
int i=0;
dest=(char*)malloc(max);
//可见字符除了空格外,范围是0x21~~0x7e
while(i<max)
{
tmp=rand()%(0x7e+1);
if(tmp>=0x21 && tmp<=0x7e)
{
dest[i]=tmp;
i++;
}
}
dest[i]='\0';
dest[i-1]='D';
dest[i-2]='C';
dest[i-3]='B';
dest[i-4]='A';
char *test="ABCD";
i=is_substr(dest,test);
printf("%d\n",i);
finish=clock();
printf("%.3lf\n",((double)finish-start)/1000);
return 0;
}
感觉这个判断一个字串是否在另一个字串中的函数还可以啊 