Skip to content

Instantly share code, notes, and snippets.

@RyanBreaker
Created February 16, 2016 04:11
Show Gist options
  • Save RyanBreaker/9976990bb7f23bde081d to your computer and use it in GitHub Desktop.
Save RyanBreaker/9976990bb7f23bde081d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[64];
char *p;
// Is 'orig' even in 'str'?
if(!(p = strstr(str, orig)))
return str;
// Copy characters from 'str' start to 'orig' st$
strncpy(buffer, str, p-str);
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
int main() {
puts(replace_str("Hello, worlddddd!", "worlddddd", "Miamii"));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment