不区分大小写的strCaseStr---改造strstr

      直接上菜:

 

#include <iostream>
using namespace std;

char *strCaseStr(const char *str1, const char *str2)
{
	if(NULL == str1 || NULL == str2)
	{
		return NULL;
	}

	int len1 = strlen(str1);
	int len2 = strlen(str2);

	char *pStr1 = (char *)malloc(len1 + 1);
	char *pStr2 = (char *)malloc(len2 + 1);
	memset(pStr1, 0, len1 + 1); // 勿忘记
	memset(pStr2, 0, len2 + 1); // 勿忘记

	strcpy(pStr1, str1);
	strcpy(pStr2, str2);

	strlwr(pStr1);
	strlwr(pStr2);

	char *p = strstr(pStr1, pStr2);
	if(NULL == p)
	{
		delete pStr1;
		delete pStr2;
		return NULL;
	}

	char *pRet = (char *)(str1 + (p - pStr1));
	delete pStr1;
	delete pStr2;

	return pRet;
}


int main()
{
	char testStr1[][100] = 
	{
		"abc", "abc", "Abc", "Abc", "Abc"
	};

	char testStr2[][100] =
	{
		"bc", "Bc", "Bc", "BC", "ac"
	};


	int size = sizeof(testStr1) / sizeof(testStr1[0]);
	int i = 0;
	char *p = NULL;
	for(i = 0; i < size; i++)
	{
		if(NULL != (p = strCaseStr(testStr1[i],  testStr2[i])))
		{
			cout << p << endl;
		}
		else
		{
			cout << "error" << endl; 
		}
	}

	return 0;
}


         但是要注意, 下面代码是有错误的:

 

 

#include <iostream>
using namespace std;

char *strCaseStr(const char *str1, const char *str2)
{
	if(NULL == str1 || NULL == str2)
	{
		return NULL;
	}

	int len1 = strlen(str1);
	int len2 = strlen(str2);

	char *pStr1 = (char *)malloc(len1 + 1);
	char *pStr2 = (char *)malloc(len2 + 1);
	memset(pStr1, 0, len1 + 1); // 勿忘记
	memset(pStr2, 0, len2 + 1); // 勿忘记

	strcpy(pStr1, str1);
	strcpy(pStr2, str2);

	strlwr(pStr1);
	strlwr(pStr2);

	char *p = strstr(pStr1, pStr2);
	if(NULL == p)
	{
		delete pStr1;
		delete pStr2;
		return NULL;
	}

	char *pRet = (char *)(str1 + (p - pStr1));
	delete pStr1;
	delete pStr2;

	return pRet;
}


int main()
{
	char testStr1[][100] = 
	{
		"abc", "abc", "Abc", "Abc", "Abc"
	};

	char testStr2[][100] =
	{
		"bc", "Bc", "Bc", "BC", "ac"
	};


	int size = sizeof(testStr1) / sizeof(testStr1[0]);
	int i = 0;
	char *p = NULL;
	for(i = 0; i < size; i++)
	{
		cout << strCaseStr(testStr1[i],  testStr2[i]) << endl; // error
	}

	return 0;
}

          

 

       不早了啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值