Demonstrate string duplication (manual) Dry Run in C

Demonstrate string duplication (manual) 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.

Demonstrate string duplication (manual) Program Code

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

char* duplicateString(const char* source) {
    // Calculate length
    int len = strlen(source);
    
    // Allocate memory for new string (+1 for null terminator)
    char* dest = (char*)malloc((len + 1) * sizeof(char));
    
    if(dest == NULL) {
        printf("Memory allocation failed!\n");
        return NULL;
    }
    
    // Copy characters
    for(int i = 0; i <= len; i++) {
        dest[i] = source[i];
    }
    
    return dest;
}

int main() {
    char original[] = "Dynamic String Duplication";
    char* duplicate = duplicateString(original);
    
    if(duplicate != NULL) {
        printf("Original: %s\n", original);
        printf("Duplicate: %s\n", duplicate);
        printf("Addresses are different: %p vs %p\n", original, duplicate);
        
        // Free allocated memory
        free(duplicate);
    }
    
    return 0;
}

View the complete String Functions programs page.

Program Console Demonstrate string duplication (manual) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.