#include<iostream>
#include<string.h>
using namespace std;
int delete_sub_str(const char *strFather, const char *strSon, char *strResult)
{
int istrNum = 0;
int iFatherLen = strlen(strFather);
int iSonLen = strlen(strSon);
char *strTemp = new char[iSonLen+1];
int iSonLenTemp = iSonLen;
int iTemp =0 ;
int iRcode =0;
int iResult = 0;
for(int i=0;i<iFatherLen;i++)
{
if((i+iSonLen) > iFatherLen)
{
strResult[iResult++] = strFather[i];
}
else
{
while(( iSonLenTemp -- ) > 0)
{
strTemp[iTemp] = strFather[i + iTemp];
iTemp ++;
}
strTemp[iTemp] = '\0';
iTemp = 0;
iSonLenTemp = iSonLen;
int j =0;
for(j =0;j < iSonLen; j++)
{
if(strSon[j] != strTemp[j])
{
strResult[iResult++] = strFather[i];
break;
}
}
if(j == iSonLen)
{
i = i + iSonLen-1;
istrNum++;
}
}
}
strResult[iResult] = '\0';
delete[]strTemp;
return istrNum;
}
int main()
{
char s[100];
int num = delete_sub_str("123abc12de234fg1hi34j123k","123",s);//123abc12de234fg1hi34j123k
std::cout<<"NUM:"<<num<<" "<<"Result:"<<s<<std::endl;
getchar();
return 0;
}
今天无聊的时候 写了一个小题目 删除子串
最新推荐文章于 2022-10-12 13:02:15 发布