Created
February 16, 2016 04:11
-
-
Save RyanBreaker/9976990bb7f23bde081d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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