Convert string to integer without using any in-built functions - GeeksforGeeks

27 Oct, 2021
Given a string str, the task is to convert the given string into the number without using any inbuilt function.
Examples: 
Input: str = “985632”Output: 985632Explanation:Given input is in string form and returned output is in integer form.
Input: str = “123”Output: 123Explanation:Given input is in string form and returned output is in integer form.
Approach: The idea is to use the ASCII value of the digits from 0 to 9 start from 48 – 57.Therefore, to change the numeric character to an integer subtract 48 from the ASCII value of the character will give the corresponding digit for the given character.
Below is the implementation of the above approach:
C++
C
Java
Python3
C#
// C++ program for the above approach #include <iostream> using namespace std;   // Function to convert string to // integer without using functions void convert(string s) {     // Initialize a variable     int num = 0;     int n = s.length();       // Iterate till length of the string     for (int i = 0; i < n; i++)           // Subtract 48 from the current digit         num = num * 10 + (int(s[i]) - 48);       // Print the answer     cout << num; }   // Driver Code int main() {     // Given string of number     char s[] = "123";       // Function Call     convert(s);     return 0; } 
// C program for the above approach #include <stdio.h> #include <string.h>   // Function to convert string to // integer without using functions void convert(char s[]) {     // Initialize a variable     int num = 0;     int n = strlen(s);       // Iterate till length of the string     for (int i = 0; i < n; i++)           // Subtract 48 from the current digit         num = num * 10 + (s[i] - 48);       // Print the answer     printf("%d", num); }   // Driver Code int main() {     // Given string of number     char s[] = "123";       // Function Call     convert(s);     return 0; } 
// Java program for the above approach class GFG{   // Function to convert string to // integer without using functions public static void convert(String s) {           // Initialize a variable     int num = 0;     int n = s.length();       // Iterate till length of the string     for(int i = 0; i < n; i++)           // Subtract 48 from the current digit         num = num * 10 + ((int)s.charAt(i) - 48);       // Print the answer     System.out.print(num); }   // Driver code public static void main(String[] args) {     // Given string of number     String s = "123";       // Function Call     convert(s); } }   // This code is contributed by divyeshrabadiya07 
# Python3 program for the above approach   # Function to convert string to # integer without using functions def convert(s):       # Initialize a variable     num = 0    n = len(s)       # Iterate till length of the string     for i in s:           # Subtract 48 from the current digit         num = num * 10 + (ord(i) - 48)       # Print the answer     print(num)   # Driver code if __name__ == '__main__':       # Given string of number     s = "123"      # Function Call     convert(s)   # This code is contributed by Shivam Singh 
// C# program for the above approach using System;  class GFG{  // Function to convert string to // integer without using functions public static void convert(string s) {           // Initialize a variable     int num = 0;     int n = s.Length;       // Iterate till length of the string     for(int i = 0; i < n; i++)           // Subtract 48 from the current digit         num = num * 10 + ((int)s[i] - 48);       // Print the answer     Console.Write(num); }   // Driver codepublic static void Main(string[] args){          // Given string of number     string s = "123";       // Function call     convert(s); }}  // This code is contributed by rock_cool
123

Time Complexity: O(N), where N is the length of the given string.Auxiliary Space: O(1)
SHIVAMSINGH67
divyeshrabadiya07
rock_cool
nidhi_biet
saurabh1990aror
number-digits
Numbers
Arrays
C Programs
C++ Programs
Greedy
Mathematical
Strings
Arrays
Strings
Greedy
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Building Heap from Array
Move all negative numbers to beginning and positive to end with constant extra space
Strings in C
Arrow operator -> in C/C++ with Examples
UDP Server-Client implementation in C
C Program to read contents of Whole File
Header files in C/C++ and its uses