C語言實(shí)現(xiàn)字符串替換函數(shù):
為漢壽等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及漢壽網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、漢壽網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
#include stdio.h
#include stdlib.h
#include ctype.h
#include string.h
//字符串替換函數(shù)
/********************************************************************
* Function: my_strstr()
* Description: 在一個(gè)字符串中查找一個(gè)子串;
* Input: ps: 源; pd:子串
* Return : 0:源字符串中沒有子串; 1:源字符串中有子串;
*********************************************************************/
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')
{
return (ps - c);
}
c = 0;
pd = pt;
}
return 0;
}
/********************************************************************
* Function: memcpy()
* Description: 復(fù)制一個(gè)內(nèi)存區(qū)域到另一個(gè)區(qū)域;
* Input: src: 源;
count: 復(fù)制字節(jié)數(shù).
* Output: dest: 復(fù)制目的地;
* Return : dest;
*********************************************************************/
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: 在一個(gè)字符串中查找一個(gè)子串,并且把所有符合的子串用
另一個(gè)替換字符串替換。
* Input: p_source:要查找的母字符串; p_seach要查找的子字符串;
p_repstr:替換的字符串;
* Output: p_result:存放結(jié)果;
* Return : 返回替換成功的子串?dāng)?shù)量;
* Others: p_result要足夠大的空間存放結(jié)果,所以輸入?yún)?shù)都要以\0結(jié)束;
*********************************************************************/
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++; //匹配子串計(jì)數(shù)加1;
printf("結(jié)果:%s\r\n",p_result);
printf("源字符:%s\r\n",p_source);
// 拷貝上一個(gè)替換點(diǎn)和下一個(gè)替換點(diǎn)中間的字符串
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};//存放替換結(jié)果;
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個(gè)子字符串;\r\n",i);
printf("替換后結(jié)果:%s\r\n",result_a);
system("pause");
}
1、首先輸入代碼:
#include string.h
#include stdio.h
/**
* 參數(shù):
* originalString[] :原始字符串
* key[] : 待替換的字符串
* swap[] : 新字符串
*/
void replace(char originalString[], char key[], char swap[]){
int lengthOfOriginalString, lengthOfKey, lengthOfSwap, i, j , flag;
char tmp[1000];
2、然后輸入:
//獲取各個(gè)字符串的長度
lengthOfOriginalString = strlen(originalString);
lengthOfKey = strlen(key);
lengthOfSwap = strlen(swap);
for( i = 0; i = lengthOfOriginalString - lengthOfKey; i++){
flag = 1;
//搜索key
for(j? = 0; j lengthOfKey; j ++){
if(originalString[i + j] != key[j]){
flag = 0;
break;
}
}
3、然后輸入:
//如果搜索成功,則進(jìn)行替換
if(flag){
strcpy(tmp, originalString);
strcpy(tmp[i], swap);
strcpy(tmp[i + lengthOfSwap], originalString[i? + lengthOfKey]);
strcpy(originalString, tmp);
i += lengthOfSwap - 1;
lengthOfOriginalString = strlen(originalString);
}
}
}
4、然后輸入:
/**
* main function
*/
int main(){
char originalString[1000] = {"abcfffffabcfffffabcfffff"};
char key[] = {"abc"};
char swap[] = {"aabbcc"};
replace(originalString, key, swap);
printf("%s\n", originalString);
return 0;
}
5、這樣就完成了。
#include stdio.h
#define N 80
/* 請?jiān)谶@里填寫答案 */
void f(char *c,char a)
{
for(;*c;c++)
? if(*c='0'*c='9')
? ? ? *c=a;
}
int main(void)
{
char c[N], a;
gets(c);
scanf("%c", a);
f(c, a);
printf("%s", c);
return 0;
}
1、C語言中沒有提供字符串替換函數(shù),網(wǎng)上能找到的類似函數(shù)也只是能替換一個(gè),不能替換全部,工作中卻常常要用到這個(gè)功能,故實(shí)現(xiàn)一個(gè)函數(shù)。該函數(shù)所使用到的相關(guān)函數(shù)均是自己實(shí)現(xiàn),沒有調(diào)用庫函數(shù)。
2、函數(shù)代碼如下:
/*descript:replace?str,返回一個(gè)替換以后的字符串,用完之后要free()
success:return?1
error:return?0
BUG:"select?*?from?tab?where?id=':a'?and?name?=':aa'",this?is?not?support,this?function?is?just?simple?str_replace?,not?support?all?SQL?language
*/
char?*replacestr(char?*strbuf,?char?*sstr,?char?*dstr)
{???????char?*p,*p1;
int?len;
if?((strbuf?==?NULL)||(sstr?==?NULL)||(dstr?==?NULL))
return?NULL;
p?=?strstr(strbuf,?sstr);???????//返回字符串第一次出現(xiàn)的地址,否則返回NULL
if?(p?==?NULL)??/*not?found*/
return?NULL;
len?=?strlen(strbuf)?+?strlen(dstr)?-?strlen(sstr);
p1?=?malloc(len);
bzero(p1,?len);
strncpy(p1,?strbuf,?p-strbuf);
strcat(p1,?dstr);
p?+=?strlen(sstr);
strcat(p1,?p);
return?p1;
}