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
請按任意鍵繼續. . .