Created
December 22, 2011 14:00
-
-
Save silv3rm00n/1510389 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Search and replace a string with another string , in a string | |
* */ | |
char *str_replace(char *search , char *replace , char *subject) | |
{ | |
char *new_subject = NULL , *p , *temp; | |
int size; | |
//subject to work upon | |
new_subject = strdup(subject); | |
//temp needs to be max as big as original subject , it will always hold a part of unconverted string | |
temp = malloc(strlen(subject)); | |
while( (p = strstr( new_subject , search)) ) | |
{ | |
size = strlen(new_subject) + 1 + strlen(replace) - strlen(search); | |
new_subject = (char*) realloc(new_subject , size ); | |
//Copy forward string to temp; | |
sprintf(temp , "%s" , p + strlen(search)); | |
//write replacement to subject | |
strcpy(p , replace); | |
//write back temp string | |
sprintf( p+strlen(replace) , "%s" , temp); | |
} | |
return new_subject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment