简单实现ReplaceAll(转)

int GetFindStrCount(char* src, char* find)
{  
int count = 0;  
char* position =src;  
int findLen = strlen(find);  
while((position = strstr(position, find)) != NULL)
{  
count++;  
position = position + findLen;  
}  
return count;  
}


/************************************************************************/
/* @param src  要替换的字符串   
/* @param des  替换后的字符串存放
/* @param find 所要替换的字符串
/* @param replaceWith 要替换的字符串
/************************************************************************/
char* ReplaceAll(char* src,char *des, char* find, char* replaceWith)
{  
//如果find或者replace为null,则返回和src一样的字符串。  
if(find == NULL || replaceWith == NULL)
{  
return strdup(src);  
}  
//指向替换后的字符串的head。  
char* afterReplaceHead = NULL;  
//总是指向新字符串的结尾位置。  
char* afterReplaceIndex = NULL;  
//find字符串在src字符串中出现的次数  
int count = 0;  
int i,j,k;  


int srcLen = strlen(src);  
int findLen = strlen(find);  
int replaceWithLen = strlen(replaceWith);  


//指向src字符串的某个位置,从该位置开始复制子字符串到afterReplaceIndex,初始从src的head开始复制。  
char* srcIndex = src;  
//src字符串的某个下标,从该下标开始复制字符串到afterReplaceIndex,初始为src的第一个字符。  
int cpStrStart = 0;  


//获取find字符串在src字符串中出现的次数  
count = GetFindStrCount(src, find);  
//如果没有出现,则返回和src一样的字符串。  
if(count == 0)
{  
return strdup(src);  
}  


//为新字符串申请内存  
afterReplaceHead = afterReplaceIndex = des; 
//初始化新字符串内存  
memset(afterReplaceHead, '\0',sizeof(afterReplaceHead));  


for(i = 0,j = 0,k = 0;i!=srcLen;i++)
{  
//如果find字符串的字符和src中字符串的字符是否相同。  
if(src[i] == find[j])
{  
//如果刚开始比较,则将i的值先赋给k保存。  
if(j == 0)
{  
k = i;  
}  
//如果find字符串包含在src字符串中  
if(j == (findLen-1))
{  
j = 0;  
//拷贝src中find字符串之前的字符串到新字符串中  
strncpy(afterReplaceIndex, srcIndex, i - findLen - cpStrStart + 1);  
//修改afterReplaceIndex  
afterReplaceIndex = afterReplaceIndex + i - findLen - cpStrStart + 1;  
//修改srcIndex  
srcIndex = srcIndex + i - findLen - cpStrStart + 1;  
//cpStrStart  
cpStrStart = i + 1;               


//拷贝replaceWith字符串到新字符串中  
strncpy(afterReplaceIndex, replaceWith, replaceWithLen);  
//修改afterReplaceIndex  
afterReplaceIndex = afterReplaceIndex + replaceWithLen;  
//修改srcIndex  
srcIndex = srcIndex + findLen;  
}
else
{  
j++;  
}  
}
else
{  
//如果find和src比较过程中出现不相等的情况,则将保存的k值还给i  
if(j != 0)
{  
i = k;  
}  
j = 0;  
}  
}  
//最后将src中最后一个与find匹配的字符串后面的字符串复制到新字符串中。  
strncpy(afterReplaceIndex, srcIndex, i - cpStrStart);  


return afterReplaceHead;  
}  


转载于:https://my.oschina.net/Cosco/blog/333016

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值