
#include <malloc.h>
#include <memory.h>
#include <string.h>
char *replace(char *str, const char *before, const char* after)
{
if (strcmp(before, after) == 0)
return str
char *ps = str
char* tmps
int fl
while (ps = strstr(ps, before))
{
tmps = (char*)malloc(ps - str + strlen(after) + 1)
memcpy(tmps, str, ps - str)
*(tmps + (ps - str)) = 0
strcat(tmps, after)
ps += strlen(before)
tmps=(char*)realloc(tmps, strlen(tmps) + strlen(ps) + 1)
fl = strlen(tmps)
strcat(tmps, ps)
strcpy(str, tmps)
ps = str + fl
free(tmps)
}
return str
}
int main()
{
char str[200] = "1223344222333444"
printf("%s\n", replace(str, "22", "ABC"))
return 0
}
哦,不能用strstr和memcpy啊,改了下:
#include<stdio.h>
#include <malloc.h>
#include <string.h>
char* fstr(char* str, const char* subs)
{
char* t = (char*)malloc(strlen(subs) + 1)
char* ps = str
int i
int slen = strlen(subs)
*(t + slen) = 0
while (strlen(ps) >= slen)
{
for (i = 0 i < slen ++i)
{
*(t + i) = *(ps + i)
}
if (strcmp(t, subs) == 0)
{
free(t)
return ps
}
++ps
}
free(t)
return NULL
}
char *replace(char *str, const char *before, const char* after)
{
if (strcmp(before, after) == 0)
return str
char *ps = str
char* tmps
int fl,i
while (ps = fstr(ps, before))
{
tmps = (char*)malloc(ps - str + strlen(after) + 1)
for (i = 0 i < ps - str ++i)
*(tmps + i) = *(str + i)
*(tmps + (ps - str)) = 0
strcat(tmps, after)
ps += strlen(before)
tmps = (char*)realloc(tmps, strlen(tmps) + strlen(ps) + 1)
fl = strlen(tmps)
strcat(tmps, ps)
strcpy(str, tmps)
ps = str + fl
free(tmps)
}
return str
}
int main()
{
char str[200] = "1223344222333444"
printf("%s\n", replace(str, "22", "ABC"))
return 0
}
C语言实现字符串替换函数:#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
//字符串替换函数
/********************************************************************
* Function: my_strstr()
* Description: 在一个字符串中查找一个子串
* 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: 复制一个内存区域到另一个区域
* Input: src: 源
count: 复制字节数.
* Output: dest: 复制目的地
* 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: 在一个字符串中查找一个子串,并且把所有符合的子串用
另一个替换字符串替换。
* Input: p_source:要查找的母字符串; p_seach要查找的子字符串
p_repstr:替换的字符串
* Output: p_result:存放结果
* Return : 返回替换成功的子串数量
* Others: p_result要足够大的空间存放结果,所以输入参数都要以\0结束
*********************************************************************/
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")
}
说一下实现步骤和伪代码BOOL Exchange(){
//这里用fstream流首先读入你的txt文件到str中
ifstream fresult
char str[50];//存储txt文件里的字符串50可根据大小改变
fresult.open("123.txt",ios::in)
if(!fresult){cout<<"erorr"<<endl}
int i=0
while(fresult>>str[i]){
i++
}
fresult.close()
//把字符串读入str中了就可以对它进行 *** 作了
for(int j=0;j<50j++){
if(str[j]==A) str[j]=B
}
//这样把str在重新写入123.txt里就行了
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)