Open In App
Related Articles

C Program to Replace a Word in a Text By Another Given Word

Improve Article
Save Article
Like Article

Given three strings ‘str’, ‘oldW’ and ‘newW’. The task is find all occurrences of the word ‘oldW’ and replace then with word ‘newW’. Examples:

Input : str[] = "xxforxx xx for xx", 
        oldW[] = "xx", 
        newW[] = "geeks"
Output : geeksforgeeks geeks for geeks

The idea is to traverse the original string and count the number of times old word occurs in the string. Now make a new string of sufficient size so that new word can be replaced. Now copy original string to new string with replacement of word. 



// C program to search and replace
// all occurrences of a word with
// other word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to replace a string with another
// string
char* replaceWord(const char* s, const char* oldW,
                const char* newW)
    char* result;
    int i, cnt = 0;
    int newWlen = strlen(newW);
    int oldWlen = strlen(oldW);
    // Counting the number of times old word
    // occur in the string
    for (i = 0; s[i] != '\0'; i++) {
        if (strstr(&s[i], oldW) == &s[i]) {
            // Jumping to index after the old word.
            i += oldWlen - 1;
    // Making new string of enough length
    result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);
    i = 0;
    while (*s) {
        // compare the substring with the result
        if (strstr(s, oldW) == s) {
            strcpy(&result[i], newW);
            i += newWlen;
            s += oldWlen;
            result[i++] = *s++;
    result[i] = '\0';
    return result;
// Driver Program
int main()
    char str[] = "xxforxx xx for xx";
    char c[] = "xx";
    char d[] = "Geeks";
    char* result = NULL;
    // oldW string
    printf("Old string: %s\n", str);
    result = replaceWord(str, c, d);
    printf("New String: %s\n", result);
    return 0;


Old string: xxforxx xx for xx
New String: GeeksforGeeks Geeks for Geeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2: This method involves the in-place update of the string. It is more efficient as it uses only extra space for the new characters to be inserted. 



// C Program to replace a word in a text by another given
// word by inplace updation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replaceWord(char* str, char* oldWord, char* newWord)
    char *pos, temp[1000];
    int index = 0;
    int owlen;
    owlen = strlen(oldWord);
    // Repeat This loop until all occurrences are replaced.
    while ((pos = strstr(str, oldWord)) != NULL) {
        // Bakup current line
        strcpy(temp, str);
        // Index of current found word
        index = pos - str;
        // Terminate str after word found index
        str[index] = '\0';
        // Concatenate str with new word
        strcat(str, newWord);
        // Concatenate str with remaining words after
        // oldword found index.
        strcat(str, temp + index + owlen);
int main()
    char str[1000], oldWord[100], newWord[100];
    printf("Enter the string: ");
    printf("Enter the word to be replaced: ");
    printf("Replace with: ");
    replaceWord(str, oldWord, newWord);
    printf("\nModified string: %s", str);
    return 0;


xxforxx xx for xx

geeksforgeeks geeks for geeks

Time Complexity: O(n)
Auxiliary Space: O(1)

Last Updated : 11 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials