63,596
社区成员




#include<iostream>
#include<cstring>
using namespace std;
void replace(char *s,char *buf,char ch,char *p)
{
char temp[1024]={0};
char *t=temp;
int nLen=strlen(p);
while(*s){
if(*s==ch){
strcpy(t,p);
++s;
t+=nLen;
continue;
}
*t++=*s++;
}
strcpy(buf,temp);
}
int main(void)
{
char s[]="1 23 4 56 78 9";
char str[64];
replace(s,str,' ',"jimmy");
cout<<str<<endl;
system("pause");
return 0;
}
一个很烂的代码,仅供参考
#include "iostream"
using namespace std;
void replace(char str[], const char pat[])
{
char temp[128];
char* ps = str;
char* pt = temp;
while ((*ps) && pt < &temp[128])
{
if ((*ps) == ' ')
{
strcpy(pt, pat);
pt += strlen(pat);
}
else
{
(*pt++) = (*ps);
}
++ps;
}
(*pt) = 0;
strcpy(str, temp);
}
int main()
{
char str[128] = "ab cd ef g";
char pat[] = "*&^";
replace(str, pat);
cout<<str<<endl;
return 0;
}