类strstr!返回值动态化。
// strstr_mine.cpp : Defines the entry point for the console application.
//
#include <cassert> //断言头文件;这些头文件包含不能放在stdafx前面!!!
#include <iostream> //cerr<< 的应用!
#include <iomanip>
#include <cstring> //对应于C 语言中的 string.h
using namespace std;
/*//find one forSch from src's begining;
char* my_strstr(const char* src, const char* forSch )
{
assert(strlen(src) >strlen(forSch));
int srcLength =strlen(src);
int forSchLength =strlen(forSch);
//char *temp=new char[forSchLength+1];
for(int i=0; i<srcLength-forSchLength+1; i++)
{
//strncpy(temp, src+i, forSchLength);
//temp[forSchLength]='\0';
//if(!strcmp(temp, forSch)){
// delete []temp;
// return (char*)(src+i);
//}
for(int j=0; j<forSchLength; j++)
{
if(*(src+j) !=*(forSch+j))
break;
}
if (j==forSchLength) {
return (char*)src;
}
src +=1;
}
//delete []temp;
return NULL;
}
*/
/*
//find one forSch from indexth match string's next;
char* my_strstr(const char* src, const char* forSch, int index=0)
{
assert(strlen(src) >strlen(forSch));
int srcLength =strlen(src);
int forSchLength =strlen(forSch);
int haveFoundNum=0;
for(int i=0; i<srcLength-forSchLength+1; i++)
{
for(int j=0; j<forSchLength; j++)
{
if(*(src+j) !=*(forSch+j))
break;
}
if (j==forSchLength) {
if (haveFoundNum >=index) {
return (char*)src;
}
haveFoundNum +=1;
}
src +=1;
}
return NULL;
}
*/
// find all forSch_es from index match string's back;
// 用一个unsigned long 数组保存 字符串指针;
// unsigned long* pstrarray =new unsigned long[1];返回数据个数动态化;
// 另外可以增加一个引用参数来记录,在函数内新创建的数组个数。
char* my_strstr(const char* src, const char* forSch, int index=0,
int wannaNum=1, void** rpstrarray=NULL)
{
assert(strlen(src) >strlen(forSch));
int srcLength =strlen(src);
int forSchLength =strlen(forSch);
int haveFoundNum=0;
unsigned long* pstrarray =(unsigned long*)(*rpstrarray);
cout<< "111 pstrarray's addr: "<<hex<<(unsigned long)pstrarray<<endl;
if (wannaNum >1) {
delete []pstrarray;
pstrarray =new unsigned long[wannaNum];
}
cout<< "222 pstrarray's addr: "<<hex<<(unsigned long)pstrarray<<endl;
//wannaNum=4;不知道为何,重新分配 堆空间地址是一样的(从上面的两个cout可以看出);
//而实际情况,应该很多时候都不会一样的;
//让指针指向重新分配的地址;这个很重要,要不在调用函数delete,一般都会出错
(*rpstrarray) =pstrarray; //***********;
for(int i=0; i<srcLength-forSchLength+1; i++)
{
for(int j=0; j<forSchLength; j++)
{
if(*(src+j) !=*(forSch+j))
break;
}
if (j==forSchLength) {
if (haveFoundNum >=index) {
if (wannaNum >1) {
*(pstrarray+haveFoundNum) =(unsigned long)src;
}
else{
*(pstrarray+haveFoundNum) =(unsigned long)src;
return (char*)src;
}
}
wannaNum -=1;
haveFoundNum +=1;
}
src +=1;
}
return NULL;
}
int main(int argc, char* argv[])
{
char* pstrTest ="erifsdk_ABCkfleif_ABCkfleif_ABCkfleif_ABCkfleif_ABCkfleif_ABCkdfjoel";
char pforSch[]="_ABC";
char* pfoundPos =strstr(pstrTest, pforSch);
int tryTimes=4;
unsigned long* pstrarray =new unsigned long[1]; //************;见注释
cout<< "1 pstrarray's addr: "<<hex<<(unsigned long)pstrarray<<endl;
char* pfoundPosScd =my_strstr(pstrTest, pforSch, 0, tryTimes, (void**)(&pstrarray));
if (pfoundPos && pfoundPosScd) {
cout<< setw(0x10)<<left<< (unsigned long)pstrTest <<pstrTest <<endl
<< setw(0x10)<<left<< (unsigned long)pfoundPos <<pfoundPos <<endl
<< setw(0x10)<<left<< (unsigned long)pfoundPosScd <<pfoundPosScd <<endl;
for(int i=0; i<tryTimes; i++)
{
cout<< (pstrarray[i])<<"\t"<<(char*)(pstrarray[i])<<endl;
}
}
cout<< "2 pstrarray's addr: "<<hex<<(unsigned long)pstrarray<<endl;
delete []pstrarray;
return 0;
}
/*在被调用函数里面,如果没有了“(*rpstrarray) =pstrarray;”这句的话,
*并且在调用函数中 “unsigned long* pstrarray =new unsigned long[2];”
*的中括号里面为2的话,在被调用函数里面new 分配的地址是之前delete的地址一样的;
*所以也不会出错;只是一种巧合。
*而当中括号里数值为1 时,就会出错了;
*/