Saturday 11 November 2017

C Program to Search a Word & Replace it with the Specified Word


Code:

#include
#include
#include

/*Function to replace a string with another string*/

char *rep_str(const char *s, const char *old, const char *new1)
{
    char *ret;
    int i, count = 0;
    int newlen = strlen(new1);
    int oldlen = strlen(old);

    for (i = 0; s[i] != '\0'; i++)    
    {
        if (strstr(&s[i], old) == &s[i]) 
        {
            count++;
            i += oldlen - 1;
        }
    }
    ret = (char *)malloc(i + count * (newlen - oldlen));
    if (ret == NULL)
        exit(EXIT_FAILURE);
    i = 0;
    while (*s)
    {
        if (strstr(s, old) == s) //compare the substring with the newstring
        {
            strcpy(&ret[i], new1);
            i += newlen; //adding newlength to the new string
            s += oldlen;//adding the same old length the old string
        }
        else
        ret[i++] = *s++;
    }
    ret[i] = '\0';
    return ret;
}

int main(void)
{
    char mystr[100], c[10], d[10];
    printf("Enter a string along with characters to be rep_strd:\n");
    gets(mystr);
    printf("Enter the character to be rep_strd:\n");
    scanf(" %s",c);
    printf("Enter the new character:\n");
    scanf(" %s",d);
    char *newstr = NULL;

    puts(mystr);
    newstr = rep_str(mystr, c,d);
    printf("%s\n", newstr);
    free(newstr);
    return 0;
}


Output:

Enter a string along with characters to be rep_strd:
prrrogram C prrrogramming
Enter the character to be rep_strd:
rr
Enter the new character:
mmm
prrrogram C prrrogramming
pmmmrogram C pmmmrogramming




More C Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...