Link to home
Create AccountLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

Find an replace multiple word in a string

Hi Experts,

I have a string replacing function that needs to find and replace multiple words in a string.
I use the following function and it seems to be fine sometimes, but at times it is core dumping.  I can't seemed to understand why.  The memory allocation seems to be fine.  It core dumps little after calling this function.

For example, I want to replace all the %s with "one" in the following functions.  It is initially plan to be used with sprintf, but I don't know the number of % appear in the string so I decided do replace this using the function.

string1 = "This is string %. Plus this % too"
string2 = "This another %s is different"

bool replace_multi_words(const char *str, const char *orig, const char *rep, char* buffer)
{
    char *p = NULL;
    unsigned count = 0;
    unsigned offset = 0;
    int len = 0;
    int needed = 0;
    char *temp = malloc(strlen(str)+ 2*strlen(rep)+1);
    needed = strlen(str)+ 2*strlen(rep)+1;
    strlcpy(temp, str, strlen(str) + 1);

    while ((p = strstr(temp + offset, orig))) {
        offset = p - temp;
        strlcpy(buffer, temp, offset+1);
        buffer[offset] = '\0';
        len = strlen(rep) + strlen(p + strlen(orig)) + 1;
        snprintf(buffer + offset, len, "%s%s", rep, p + strlen(orig));
        offset += strlen(rep);
        strlcpy(temp, buffer, strlen(buffer)+1);
        count++;
    }
    free(temp);
    return count ? true : false;
}

Open in new window


It is called by the following code... where id is an int like 123 and which is changed to a string "123"

replace_multi_words(pattern, "%s", itoa(id, strid, 10), buffer );

Open in new window

Avatar of jkr
jkr
Flag of Germany image

You should change the calling code to

replace_multi_words(pattern, "%%s", itoa(id, strid, 10), buffer );

Open in new window


to achieve this - the '%' sing has to be doubled, otherwise it is interpreted like in a format string.
Avatar of ambuli

ASKER

Thanks JKR.  Can you explain why it works sometime though?
Avatar of ambuli

ASKER

Also, it seem to have working for "//n" to be replaced with "/n".  Like below.  Would it work?

replace_multi_words(str, "\\n", "\n", buffer)
Avatar of ambuli

ASKER

Hi JKR,

Sorry, I made a typo in the question.  The strings are like the ones below and I want to replace the '%s' with another word.  I don't have control over this string, it is given from another component.  It used to work because we only has one '%s' in the string before and we used sprintf.  But, now it may be one or two that is why I took this route.  thanks and sorry for the confusion.

string1 = "This is string %s. Plus this %s too"
string2 = "This another %s is different"
Yes, that would work. BTW, the above was a misconception of mince this is only necessary when you are actually working with format strings.. BTW, I just tried your code, and it seems to be working fine with

char pattern[128] = "This another %s is a different %s";
replace_multi_words(pattern, "%s", "one", buffer );

Open in new window


and the output was

This another one is a different one

Open in new window

Avatar of ambuli

ASKER

Is there an alternative way of doing this?
If you want to stay with plain C, not really, all ways will end up somehow similar. If you are up to C++'s STL classes, that could be a lot easier.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ambuli

ASKER

Thanks JKR.