#include<stdio.h>
#include<time.h>
char *find_char_1(char *source,char *chars);
char *find_char_2(char *source,char *chars);
int main(){
char *p=NULL;
char src[]="123456789";
char chr[]="-----9";
clock_t start,end;
start=clock();
for(int i=0;i<50000000;p=find_char_1(src,chr),i++);
end=clock();
printf("1 cost %lf seconds.\n",(double)(end-start)/CLOCKS_PER_SEC);
start=clock();
for(int i=0;i<50000000;p=find_char_2(src,chr),i++);
end=clock();
printf("2 cost %lf seconds.\n",(double)(end-start)/CLOCKS_PER_SEC);
if(printf("%s",(p=find_char_1(src,chr))?"FoundIt!\n":"Nothingfound!\n")==9)
printf("Index is %d.\n",(p-src));
return 0;}
//
char *find_char_1(char *source,char *chars){
char *temp=source;
if(source&&chars&&*source&&*chars)
for(;*chars;){
if(*source==*chars)
return source;
else if(!*++source){
source=temp;
chars++;
}
}
return NULL;}
char *find_char_2(char *source,char *chars){
char *temp=chars;
if(source&&chars&&*source&&*chars)
for(;*source;source++){
for(;*chars;chars++){
if(*source==*chars)
return source;
}
chars=temp;
}
return NULL;}
6.18.1-PointersOnC-20220129
于 2022-01-29 21:48:30 首次发布