C语言实现字符串替换函数
C语言中没有提供字符串替换函数,网上能找到的类似函数也只是能替换一个,不能替换全部,工作中却常常要用到这个功能,故实现一个函数。该函数所使用到的相关函数均是自己实现,没有调用库函数。
相关代码如下:
/********************************************************************
* Function: my_strstr()
* Description: 在一个字符串中查找一个子串;
* Calls: 无;
* Called By: 无
* Input: ps: 源; pd:子串
* Output: 无;
* Return : 0:源字符串中没有子串; 1:源字符串中有子串;
* Author: ChenZhiFa
* Others: 无;
* date of completion:
*********************************************************************/
char * my_strstr(char * ps,char *pd)
{
char *pt = pd;
int c = 0;
while(*ps != '\0')
{
if(*ps == *pd)
{
while(*ps == *pd && *pd!='\0')
{
ps++;
pd++;
c++;
}
}
else
{
ps++;
}
if(*pd == '\0')
{
//sum++;
return (ps - c);
}
c = 0;
pd = pt;
}
return 0;
}
/********************************************************************
* Function: memcpy()
* Description: 复制一个内存区域到另一个区域;
* Calls: 无;
* Called By: 无
* Input: src: 源;
count: 复制字节数.
* Output: dest: 复制目的地;
* Return : dest;
* Author: ChenZhiFa
* Others: 无;
* date of completion:
*********************************************************************/
void * memcpy(void * dest,const void *src,size_t count)
{
char *tmp = (char *) dest, *s = (char *) src;
while (count--)
*tmp++ = *s++;
return dest;
}
/********************************************************************
* Function: str_replace()
* Description: 在一个字符串中查找一个子串,并且把所有符合的子串用
另一个替换字符串替换。
* Calls: memcpy();
* Called By: 无
* Input: p_source:要查找的母字符串; p_seach要查找的子字符串;
p_repstr:替换的字符串;
* Output: p_result:存放结果;
* Return : 返回替换成功的子串数量;
* Author: ChenZhiFa
* Others: p_result要足够大的空间存放结果,所以输入参数都要以\0结束;
* date of completion:
*********************************************************************/
int str_replace(char *p_result,char* p_source,char* p_seach,char *p_repstr)
{
int c = 0;
int repstr_leng = 0;
int searchstr_leng = 0;
char *p1;
char *presult = p_result;
char *psource = p_source;
char *prep = p_repstr;
char *pseach = p_seach;
int nLen = 0;
repstr_leng = strlen(prep);
searchstr_leng = strlen(pseach);
do{
p1 = my_strstr(psource,p_seach);
if (p1 == 0)
{
strcpy(presult,psource);
return c;
}
c++; //匹配子串计数加1;
printf("结果:%s\r\n",p_result);
printf("源字符:%s\r\n",p_source);
// 拷贝上一个替换点和下一个替换点中间的字符串
nLen = p1 - psource;
memcpy(presult, psource, nLen);
// 拷贝需要替换的字符串
memcpy(presult + nLen,p_repstr,repstr_leng);
psource = p1 + searchstr_leng;
presult = presult + nLen + repstr_leng;
}while(p1);
return c;
}
测试代码如下:
#define MAX 200
int main(void)
{
int i = 0;
char s[MAX] ={0}; //存放源字串
char s1[MAX]={0}; //存放子字串
char s2[MAX]={0}; //存放替换字串
char result_a[2000] = {0};//存放替换结果;
char *p,*ptm,*pr;
puts("Please input the string for s:");
scanf("%s",s);
puts("Please input the string for s1:");
scanf("%s",s1);
puts("Please input the string for s2:");
scanf("%s",s2);
ptm = s;
pr = result_a;
i = str_replace(pr,ptm,s1,s2);
printf("替换%d个子字符串;\r\n",i);
printf("替换后结果:%s\r\n",result_a);
system("pause");
}
运行结果如果:
Please input the string for s:
123123123123
Please input the string for s1:
23
Please input the string for s2:
abcdefg
结果:
源字符:123123123123
结果:1abcdefg
源字符:123123123123
结果:1abcdefg1abcdefg
源字符:123123123123
结果:1abcdefg1abcdefg1abcdefg
源字符:123123123123
替换4个子字符串;
替换后结果:1abcdefg1abcdefg1abcdefg1abcdefg
请按任意键继续. . .