[ create a new paste ] login | about

Link: http://codepad.org/KCn8i8iN    [ raw code | output | fork ]

C, pasted on Jan 13:
#include <stdio.h>
#include <string.h>
 
char *replace_str(char *str, char *orig, char *rep)
{
    static char buffer[4096];
    char *p;
     
    if(!(p = strstr(str, orig)))
    return str;
     
    strncpy(buffer, str, p-str);
    buffer[p-str] = '\0';
     
    sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
     
    return buffer;
}
 
int main(void)
{
    char *str="Query Home is a good website";
    char *str1="good";
    char *str2="very good";
    
    puts(replace_str(str, str1, str2));
     
    return 0;
}


Output:
1
Query Home is a very good website


Create a new paste based on this one


Comments: