c++字符串等长替换函数
// Replace.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
int StrReplaceI(char* src,char* oldstr,char* newstr)
{
if (!src)
{
return 1;
}
int oldlen=strlen(oldstr);
int newlen=strlen(newstr);
if (newlen>oldlen)
{
return 2;
}
char *pold=StrStrI(src,oldstr);
if (!pold)
{
return 3;
}
memset(pold,' ',oldlen);
memcpy(pold,newstr,newlen);
return 0;
}
int main(int argc, char* argv[])
{
printf("Hello World!\n");
char buf[]="1234 567 890";
StrReplaceI(buf,"567","99");
printf("%s\n",buf);
return 0;
}
//
#include "stdafx.h"
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
int StrReplaceI(char* src,char* oldstr,char* newstr)
{
if (!src)
{
return 1;
}
int oldlen=strlen(oldstr);
int newlen=strlen(newstr);
if (newlen>oldlen)
{
return 2;
}
char *pold=StrStrI(src,oldstr);
if (!pold)
{
return 3;
}
memset(pold,' ',oldlen);
memcpy(pold,newstr,newlen);
return 0;
}
int main(int argc, char* argv[])
{
printf("Hello World!\n");
char buf[]="1234 567 890";
StrReplaceI(buf,"567","99");
printf("%s\n",buf);
return 0;
}