I have the following string replacement program for a string of size 256:
I am replacing the string
"\'"
with the string
'
Please suggest any modifications if needed or any mistake I have done.
#include <stdio.h>
#include <string.h>
int replace_str(char *SrcStr, char *SubStr, char *RepStr, char *FinalStr )
{
static char buffer[256]="";
char *TempStr= NULL;
char *Tmp2 = malloc(250);
memset(Tmp2,0,256);
int ret = 0;
TempStr = strstr(SrcStr, SubStr);
if ( TempStr )
{
memcpy(Tmp2,TempStr,strlen(TempStr));
strncpy(buffer, SrcStr, TempStr-SrcStr);
buffer[TempStr-SrcStr] = '\0';
sprintf(buffer+(TempStr-SrcStr), "%s%s", RepStr, Tmp2+strlen(SubStr));
puts(buffer);
free(Tmp2);
ret = replace_str(buffer, SubStr, RepStr, FinalStr);
}
else
{
memcpy(FinalStr, buffer , strlen(buffer));
}
return ret;
}
int main(void)
{
char *str1 = malloc(256);
memset(str1, 0 ,256);
memcpy(str1, "pra\'dipta\'kumar\'rout",strlen("pra\'dipta\'kumar\'rout"));
char str2[10] = "'";
char str3[3] = "\'" ;
char buff[256] ={0};
replace_str(str1, str3, str2,buff);
puts(buff);
return 0;
}