[Solved] search substring in a string


Hope the following function help you to replace first occurance

void str_find_replace(char *str, char *find, char *replace)
{
    size_t find_len,replace_len;
    char *str_end;

    find_len = strlen(find);
    replace_len = strlen(replace);
    str_end = strlen(str);

    while(*str){
        if(!strncmp(str,find,find_len) {
            memmove(str+replace_len,str+find_len, str_end - (str + find_len) + 1);
            memcpy(str,replace,replace_len);
            return;
        }
        str++;
    }
}

If you want to replace all the occurrences then use following function

void str_find_replace(char *str, char *find, char *replace)
{
    size_t find_len,replace_len;
    char *str_end;

    find_len = strlen(find);
    replace_len = strlen(replace);
    str_end = strlen(str);

    while(*str){
        if(!strncmp(str,find,find_len) {
            memmove(str+replace_len,str+find_len, str_end - (str + find_len) + 1);
            memcpy(str,replace,replace_len);
            str += replace_len;
            str_end = str + strlen(str);
            continue;
        }
        str++;
    }
}

solved search substring in a string