C Program
#include <stdio.h> int stringToInt(char str[]) { int num = 0, i = 0, sign = 1; if(str[0] == '-') { sign = -1; i++; } for(; str[i] != '\0'; i++) { num = num * 10 + (str[i] - '0'); } return num * sign; } int main() { char str[] = "-1234"; int result = stringToInt(str); printf("Converted Integer: %d", result); return 0; }
C Output
Input: "-1234" Output: Converted Integer: -1234
C++ Program
#include <iostream> using namespace std; int stringToInt(string str) { int num = 0, i = 0, sign = 1; if(str[0] == '-') { sign = -1; i++; } for(; i < str.size(); i++) { num = num * 10 + (str[i] - '0'); } return num * sign; } int main() { string str = "5678"; int result = stringToInt(str); cout << "Converted Integer: " << result; return 0; }
C++ Output
Input: "5678" Output: Converted Integer: 5678
JAVA Program
public class StringToInt { public static int stringToInt(String str) { int num = 0, i = 0, sign = 1; if(str.charAt(0) == '-') { sign = -1; i++; } for(; i < str.length(); i++) { num = num * 10 + (str.charAt(i) - '0'); } return num * sign; } public static void main(String[] args) { String str = "-890"; int result = stringToInt(str); System.out.println("Converted Integer: " + result); } }
JAVA Output
Input: "-890" Output: Converted Integer: -890
Python Program
def stringToInt(s): num, sign, i = 0, 1, 0 if s[0] == '-': sign, i = -1, 1 for j in range(i, len(s)): num = num * 10 + (ord(s[j]) - ord('0')) return num * sign s = "4321" print("Converted Integer:", stringToInt(s))
Python Output
Input: "4321" Output: Converted Integer: 4321
In-Depth Explanation
Example
When we enter the string "1234", we begin with num = 0. We process each digit: '1' → 1, then num = 0*10 + 1 = 1. Then '2' → 2, now num = 1*10 + 2 = 12. Then '3' → 123, and lastly '4' → 1234. If the string is "-1234", we observe the negative sign and just multiply by -1 at the end.
Real-Life Analogy
Just as you read numbers as words, for example, the odometer with "4 5 6", you don't read them as letters but as digits that make up the number 456. That is how computers retain text input as characters (ASCII/Unicode) and how we manually convert every digit of the character to its integer form before assembling them. Like counting cash, where "₹5, ₹0, ₹0" is not three different numbers but ₹500 if put together.
Why It Matters
This program is crucial since in most systems, numbers are received as strings. For instance, when you input in a form or get data from APIs or files, everything is read as text. To actually process, compare, or calculate values, you must convert from string to integer. Understanding how to do this without library functions teaches you how atoi or stoi actually function under the hood.
Learning Insights
Each digit character such as '7' also has an ASCII value (55), hence we subtract '0' (48) to arrive at integer 7.
We continue to multiply the current number by 10 and append the new digit. This mimics place values (units, tens, hundreds).
Processing negative numbers teaches one about sign handling.
The program reinforces knowledge about character encoding, ASCII values, and number conversion.
Interview Use Case
This is a popular interview question since it checks your knowledge of ASCII, loops, arithmetic, and edge conditions such as negative numbers. Interviewers tend to ask you to implement atoi() manually without making use of built-in methods. It also shows your capability in working with low-level string manipulation.
Practical Applications
Parsing input from text files or JSON/XML data.
Reading numeric values from user input in command-line tools.
Converting values in embedded systems where libraries may not be available.
Handling custom parsers for competitive programming challenges.
SEO-Optimized Closing
Understanding how to manually convert a string to an integer without using built-in functions like atoi, stoi, or int() is a fundamental programming skill. It not only strengthens your logic-building abilities but also helps in interviews where candidates are often asked to implement string to number conversion from scratch. Through this concept, newcomers acquire real-time experience with ASCII values, loops, negative number operations, and string manipulation, which are being used extensively in real-time applications such as data parsing, user input validation, and programming contests. Executing this problem will also provide you with problem-solving round confidence for coding interviews and technical tests.
Social Plugin