Hello to all. I have a problem with replacing a part of a string with another string.
The following code does not work. However, if you remove void in front of replace_str, in Code Blocks i am able to run it (WHY???) despite the fact that i still get a warning:
"argument 1 of 'puts' makes pointer from integer without a cast"
Also, this "program" only replaces the first occurance of s2 in s1 and it should replace all.
Plus, i would like to know the correct sintax to print res using printf (the comment in code does not do it).
Every part of code besides
void replace_str(char *res, char *str, char *orig, char *rep)
can be changed (according to assingment).
Any sort of help would be much appreciated.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 1024
void replace_str(char *res, char *str, char *orig, char *rep)
{
char buffer[4096];
int i;
char *p;
if(!(p=strstr(str, orig))){
for(i=*p;i<strlen(orig);i++)
res[i]=str[i];
}
else{
strncpy(buffer, str, p-str);
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
res=buffer;
}}
main()
{char res[SIZE],s1[SIZE],s2[SIZE],s3[SIZE];
scanf(" %s",s1);
scanf(" %s",s2);
scanf(" %s",s3);
puts(replace_str(res,s1, s2, s3));
//replace_str(res,s1, s2, s3);
//printf(" %s\n",res);
return 0;
}