Replace a sub-string in a string in C

Understanding String Manipulation

Replacing a sub-string in a string means finding a specific sequence of characters and replacing it with another sequence.

We will explore three different methods to achieve this in C.

Method 1: Using Loop

This method iterates through the string and replaces occurrences of the sub-string.

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

void replaceSubstring(char *str, char *oldWord, char *newWord) {
    char buffer[1000];
    char *pos;
    int index = 0, oldLen = strlen(oldWord);
    
    buffer[0] = '\0';
    while ((pos = strstr(str, oldWord)) != NULL) {
        strncat(buffer, str, pos - str);
        strcat(buffer, newWord);
        str = pos + oldLen;
    }
    strcat(buffer, str);
    strcpy(str, buffer);
}

int main() {
    char str[1000] = "Hello World!";
    replaceSubstring(str, "World", "C");
    printf("Modified String: %s", str);
    return 0;
}
            
Input: Hello World!
Output: Hello C!

Method 2: Using Recursion

This method replaces the sub-string recursively.

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

void replaceRecursive(char *str, char *oldWord, char *newWord, char *buffer) {
    char *pos = strstr(str, oldWord);
    if (!pos) {
        strcat(buffer, str);
        return;
    }
    strncat(buffer, str, pos - str);
    strcat(buffer, newWord);
    replaceRecursive(pos + strlen(oldWord), oldWord, newWord, buffer);
}

int main() {
    char str[1000] = "I love programming";
    char buffer[1000] = "";
    replaceRecursive(str, "love", "enjoy", buffer);
    printf("Modified String: %s", buffer);
    return 0;
}
            
Input: I love programming
Output: I enjoy programming

Method 3: Using String Functions

This method uses C's built-in strstr() and strcpy() functions.

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

void replaceUsingFunctions(char *str, char *oldWord, char *newWord) {
    char buffer[1000];
    char *pos = strstr(str, oldWord);
    if (!pos) return;
    
    strncpy(buffer, str, pos - str);
    buffer[pos - str] = '\0';
    strcat(buffer, newWord);
    strcat(buffer, pos + strlen(oldWord));
    strcpy(str, buffer);
}

int main() {
    char str[1000] = "Replace old text";
    replaceUsingFunctions(str, "old", "new");
    printf("Modified String: %s", str);
    return 0;
}
            
Input: Replace old text
Output: Replace new text
Strings

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python