top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C program string search and replacement?

+1 vote
488 views

Input : Query Home is a good website

Output: Query Home is a very good website

posted Jan 13, 2015 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
basically its a find and replacement for "good" to "very good". Please confirm if you are looking for the same.
I just want to add word in between a string and it should not be overwritten.

1 Answer

0 votes

Check this (http://codepad.org/KCn8i8iN )

#include <stdio.h>
#include <string.h>

char *replace_str(char *str, char *orig, char *rep)
{
    static char buffer[4096];
    char *p;

    if(!(p = strstr(str, orig)))
    return str;

    strncpy(buffer, str, p-str);
    buffer[p-str] = '\0';

    sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

    return buffer;
}

int main(void)
{
    char *str="Query Home is a good website";
    char *str1="good";
    char *str2="very good";

    puts(replace_str(str, str1, str2));

    return 0;
}
answer Jan 13, 2015 by Salil Agrawal
Similar Questions
0 votes

a and b are compared against
the threshold ‘TH’, and the nearest value is voted as output

+1 vote

How to write a c program for data signaling rate for four signals? Data signaling rate formula should be written in c language as formula cannot be inserted?

...