[CODE] [NEWBIE] Search and replace function

From: Andrew (object@ALPHALINK.COM.AU)
Date: 10/30/98


Hi. I've just created a search and replace function, to be used with colours and
other things. When I compile the code I get no warnings or errors, but the
program
crashes as soon as the function is run. Here's the code...

void search_replace(char *string, const char *find, const char *replace)
{
        char final[MAX_INPUT_LENGTH];
        size_t start, end, i;

        while (strstr(string, find) != NULL) {

                start = strstr(string, find) - string;
                end = start + strlen(find);

                for (i = 0; i < start; i++)
                        final[i] = string[i];

                strcat(final, replace);

                for (i = end; i < strlen(string); i++)
                        final[i] = string[i];

                sprintf(string, final);
        }
        return;
}

It is supposed to take three arguments -  a string to be searched, the
string it's
searching for, and what to put in place of that. Then for every occurance of
find,
it puts in replace. Any help would be appreciated. Thanks,

-- Andrew Ritchie.

| Andrew Ritchie, object@alphalink.com.au.


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST