|
|
The following function is a bit of code that I'm sure has been developed elsewhere
already, but it hard to track down. It's a string replacement function which can be
used in the code for titles, prompts, and travel messages.
void strnrplc(char * src, char * fnd, char * rpl, long n) {
char * out;
char * curr;
char * ptr;
out = (char *) malloc(sizeof(char) * n);
if ( out == NULL ) {
return;
}
curr = src;
ptr = strstr(src, fnd);
out[0] = '\0';
while ( ptr != NULL ) {
strncat( out, curr, (ptr-curr+strlen(out)) < n ? (ptr-curr) : (n-strlen(out)-1) );
strncat( out, rpl, (strlen(rpl)+strlen(out)) < n ? strlen(rpl) : (n-strlen(out)-1) );
curr = ptr + strlen(fnd);
ptr = strstr(curr, fnd);
}
strncat(out, curr, ((n-(curr-src))+strlen(out)) < n ? (n-(curr-src)) : (n-strlen(out)-1) );
out[n-1] = '\0';
strncpy(src, out, n);
free(out);
}
|