Convert string into integer (manual logic) Dry Run in C

Convert string into integer (manual logic) 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.

Convert string into integer (manual logic) Program Code

#include <stdio.h>

int stringToInt(char str[]) {
    int result = 0;
    int sign = 1;
    int i = 0;
    
    // Handle negative numbers
    if(str[0] == '-') {
        sign = -1;
        i = 1;
    }
    
    // Convert each digit
    for(; str[i] != '\0'; i++) {
        if(str[i] >= '0' && str[i] <= '9') {
            result = result * 10 + (str[i] - '0');
        } else {
            break; // Stop at non-digit
        }
    }
    
    return sign * result;
}

int main() {
    char str1[] = "12345";
    char str2[] = "-6789";
    char str3[] = "42abc";
    
    printf("'%s' as integer: %d\n", str1, stringToInt(str1));
    printf("'%s' as integer: %d\n", str2, stringToInt(str2));
    printf("'%s' as integer: %d\n", str3, stringToInt(str3));
    
    return 0;
}

View the complete String Functions programs page.

Program Console Convert string into integer (manual logic) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.