用c语言实现函数strcasestr。函数原型:char*strcasestr(const char*haystack,const char*needle) 函数返回字符串指针,指向字符串haystack中第一次出现字符串needle的开始位置,字串匹配时忽略大小写,如果没有找到字符串,返回null,完成代码后,给出5个以上的单元测试用例,以证明你的程序在各种条件下能够正确运行
代码一:
本人的代码(编译环境VC6.0) 请多多指教!
#include<iostream>
#include<string>
using namespace std;

char*strcasestr(const char*haystack,const char*needle);
char*bigtolit(const char*str);

main()


{
//五个的测试程序
cout<<strcasestr("abcDEfghi","EF")<<endl;
cout<<strcasestr("111223","11223")<<endl;
cout<<strcasestr("tshihisih","ss")<<endl;
cout<<strcasestr("tshihisih","si")<<endl;
cout<<strcasestr("tshihfsfsah","fsa")<<endl;

return 0;
}

char*strcasestr(const char*haystack,const char*needle)


{
string str = bigtolit(haystack);
string str1 = bigtolit(needle);

int pos = str.find(str1);

if(0 != (pos+1))

{
char *c = new char;
itoa((pos+1),c,10);
return c;
}
else

{
char *c = "NULL";
return c;
}
}

char*bigtolit(const char*str)


{
char *hay = new char;
memset(hay,0,sizeof(hay));

for(int i=0;i<strlen(str);i++)

{
hay[i] = tolower(str[i]);
}
return hay;
}
运行结果:
代码二:
群里IT007朋友写的
#include <string.h>
#include <stdio.h>


char* strcasestr(const char *haystack,const char *needle);

main()


{
char str1[100];
char str2[50];
char* str;



do
{
system("cls");

printf("请分别输入长度不大于100和长度不大于50的两个字符串(用空格或者回车隔开):\n");
scanf("%s%s",str1,str2);


if(strlen(str1)>100||strlen(str2)>50)
{
printf("\n对不起,你输入的字符串过长,请重新输入再来!");
continue;
}

str=strcasestr((const char*)str1,(const char*)str2);


if(!str)
{
printf("父串中查找不到与子串匹配的串!\n按Q键退出,其它任意键继续!\n");
continue;
}

printf("父串中第一个与子串匹配串的位置为:%d\n",str-str1+1);

printf("按Q键退出,其它任意键继续!\n");

}while(getch()!='q');
}




/**///////////////指针版的查找子串在源串中的位置的函数/////////////////
//加强错误处理之后的函数
//1。当子串为空时进行了处理
//2。当子串比源串要长时
//3。每次比较完之后,父串的指针只向前移动一位
//4。现在可以进行勿略大小写的判断比较了(最新)
//5。修正了一个BUG,即不能进行字母A、a、Z、z的忽略大小写的判断
//6。对忽略大小写部分的判断进行了代码,代码明显少了些(最新)

/**/////////////////////////////////////////////////////////////////////



/**//*
////////////////////////////

函数原型:char *strcasestr(const char *haystack,const char *needle)

函数功能:指向字符串haystack中第一次出现字符串needle的开始位置,
字串匹配时忽略大小写,如果没有找到字符串,返回null.

///////////////////////////
*/

char* strcasestr(const char* haystack,const char* needle)


{
int i=0;

while(*haystack&&*needle)
{
while(*haystack==*needle||\
*haystack==((*needle>='a'&&*needle<='z')?*needle-32:*needle)||\

*haystack==((*needle>='A'&&*needle<='Z')?*needle+32:*needle))
{
if(!*(++needle))return (char*)haystack-i;
if(!*(++haystack))return 0;
i++;
}
needle-=i;
//haystack=haystack-i+1;
haystack-=(i-1);
i=0;
}
return 0;
}

运行结果:
请分别输入长度不大于100和长度不大于50的两个字符串(用空格或者回车隔开):
FFadbcdddDD
DDd
父串中第一个与子串匹配串的位置为:7
按Q键退出,其它任意键继续!
地震让大伙知道:居安思危,才是生存之道。