top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Write a C program string search and replacement?

+1 vote
75 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?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...