Reverse words in a sentence Dry Run in C

Reverse words in a sentence 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.

Reverse words in a sentence Program Code

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

void reverse(char *start, char *end) {
    while(start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int main() {
    char str[] = "Hello World from C Programming";
    
    printf("Original: %s\n", str);
    
    int len = strlen(str);
    
    // Step 1: Reverse entire string
    reverse(str, str + len - 1);
    
    // Step 2: Reverse each word
    char *word_start = str;
    char *word_end = str;
    
    while(*word_end) {
        if(*word_end == ' ') {
            reverse(word_start, word_end - 1);
            word_start = word_end + 1;
        }
        word_end++;
    }
    // Reverse last word
    reverse(word_start, word_end - 1);
    
    printf("Reversed words: %s\n", str);
    return 0;
}

View the complete String Functions programs page.

Program Console Reverse words in a sentence Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.