Replace a substring with another string Dry Run in C

Replace a substring with another string is an interactive C dry run visualizer from the String Functions programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Replace a substring with another string Program Code

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

int main() {
    char str[100] = "I like Java";
    char old_substr[] = "Java";
    char new_substr[] = "C Programming";
    char result[100];
    
    printf("Original: %s\n", str);
    
    char *pos = strstr(str, old_substr);
    
    if(pos != NULL) {
        int index = pos - str;
        int len_old = strlen(old_substr);
        int len_new = strlen(new_substr);
        
        // Copy part before old substring
        strncpy(result, str, index);
        result[index] = '\0';
        
        // Append new substring
        strcat(result, new_substr);
        
        // Append remaining part after old substring
        strcat(result, pos + len_old);
        
        printf("After replacement: %s\n", result);
    } else {
        printf("Substring not found!\n");
    }
    
    return 0;
}

View the complete String Functions programs page.

Program Console Replace a substring with another string Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.