/*
题目标题:前缀判断
如下的代码判断 needle_start指向的串是否为haystack_start指向的串的前缀,如不是,则返回NULL。
比如:"abcd1234" 就包含了 "abc" 为前缀
*/
#include <stdio.h>
#include <time.h>
char* prefix(char* haystack_start, char* needle_start);
int main( )
{
/*************************************************************/
clock_t start, finish;
start = clock();
/*************************************************************/
printf("%s\n", prefix("abcd1234", "abc"));
/*************************************************************/
finish = clock();
printf( "\n\n\n程序运行时间:%.3f ms\n", (double)(finish - start));
/*************************************************************/
return 0;
}
char* prefix(char* haystack_start, char* needle_start)
{
char* haystack = haystack_start;
char* needle = needle_start;
while(*haystack && *needle){
//if(______________________________) return NULL; //填空位置
if(*haystack++ != *needle++) return NULL; //填空位置*
}
if(*needle) return NULL;
return haystack_start;
}