C Program To Write Your Own atoi()

The atoi() function in C converts a string representation of a number into an integer value. The name stands for "ASCII to Integer". While the standard library provides atoi(), implementing your own version helps understand string parsing and character-to-number conversion.

Syntax

int myAtoi(const char *str);

Parameters

  • str Pointer to the null-terminated string to be converted

Return Value

Returns the converted integer value. If no valid conversion can be performed, it returns 0.

Example 1: Basic Implementation

This example shows a simple implementation that handles positive numbers

#include <stdio.h>

int myAtoi(const char *str) {
    int result = 0;
    int i = 0;
    
    /* Skip leading whitespaces */
    while (str[i] == ' ' || str[i] == '\t' || str[i] == '<br>') {
        i++;
    }
    
    /* Convert digits to integer */
    while (str[i] >= '0' && str[i] <= '9') {
        result = result * 10 + (str[i] - '0');
        i++;
    }
    
    return result;
}

int main() {
    char str1[] = "12345";
    char str2[] = "  678";
    char str3[] = "99abc";
    
    printf("String: "%s" -> Integer: %d<br>", str1, myAtoi(str1));
    printf("String: "%s" -> Integer: %d<br>", str2, myAtoi(str2));
    printf("String: "%s" -> Integer: %d<br>", str3, myAtoi(str3));
    
    return 0;
}
String: "12345" -> Integer: 12345
String: "  678" -> Integer: 678
String: "99abc" -> Integer: 99

Example 2: Complete Implementation with Sign Handling

This enhanced version handles both positive and negative numbers

#include <stdio.h>

int myAtoi(const char *str) {
    int result = 0;
    int sign = 1;
    int i = 0;
    
    /* Skip leading whitespaces */
    while (str[i] == ' ' || str[i] == '\t' || str[i] == '<br>') {
        i++;
    }
    
    /* Handle sign */
    if (str[i] == '+' || str[i] == '-') {
        sign = (str[i] == '-') ? -1 : 1;
        i++;
    }
    
    /* Convert digits to integer */
    while (str[i] >= '0' && str[i] <= '9') {
        result = result * 10 + (str[i] - '0');
        i++;
    }
    
    return sign * result;
}

int main() {
    char str1[] = "+123";
    char str2[] = "-456";
    char str3[] = "  -789";
    char str4[] = "abc123";
    
    printf("String: "%s" -> Integer: %d<br>", str1, myAtoi(str1));
    printf("String: "%s" -> Integer: %d<br>", str2, myAtoi(str2));
    printf("String: "%s" -> Integer: %d<br>", str3, myAtoi(str3));
    printf("String: "%s" -> Integer: %d<br>", str4, myAtoi(str4));
    
    return 0;
}
String: "+123" -> Integer: 123
String: "-456" -> Integer: -456
String: "  -789" -> Integer: -789
String: "abc123" -> Integer: 0

How It Works

  1. Skip Whitespaces Move past any leading spaces, tabs, or newlines
  2. Handle Sign Check for '+' or '-' and set the sign accordingly
  3. Convert Digits Process each digit character by subtracting '0' to get numeric value
  4. Build Result Multiply previous result by 10 and add current digit
  5. Apply Sign Multiply final result by sign (1 or -1)

Key Points

  • The conversion stops at the first non-digit character
  • Leading whitespaces are ignored
  • Only the first sign character is considered
  • Returns 0 if no valid digits are found

Conclusion

Implementing your own atoi() function demonstrates fundamental string parsing techniques in C. The key is handling whitespaces, signs, and converting character digits to their numeric equivalents using ASCII arithmetic.

Updated on: 2026-03-15T14:32:09+05:30

869 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements