Compare strings using pointer Dry Run in C

Compare strings using pointer is an interactive C dry run visualizer from the Pointersarrays 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.

Compare strings using pointer Program Code

#include <stdio.h>
int stringCompare(char *str1, char *str2) {
    while(*str1 && *str2 && *str1 == *str2) {
        str1++;
        str2++;
    }
    return *str1 - *str2;
}

int stringCompareSimple(char *str1, char *str2) {
    while(*str1 == *str2) {
        if(*str1 == '\0') return 0;
        str1++;
        str2++;
    }
    return *str1 - *str2;
}

int main() {
    char str1[] = "Apple";
    char str2[] = "Apple";
    char str3[] = "Apricot";
    char str4[] = "Ape";
    
    printf("Comparing \"%s\" and \"%s\": %d\n", 
           str1, str2, stringCompare(str1, str2));
    printf("Comparing \"%s\" and \"%s\": %d\n", 
           str1, str3, stringCompare(str1, str3));
    printf("Comparing \"%s\" and \"%s\": %d\n", 
           str1, str4, stringCompare(str1, str4));
    
    printf("\nExplanation:\n");
    printf("0 = strings are equal\n");
    printf("negative = str1 < str2\n");
    printf("positive = str1 > str2\n");
    
    return 0;
}

View the complete Pointersarrays programs page.

Program Console Compare strings using pointer Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.