Convert a String to an Integer using Recursion

04 Jun, 2022
Given a string str representing a string, the task is to convert the given string into an integer.Examples:  
Input: str = “1234” Output: 1234Input: str = “0145” Output: 145  
 
Approach: Write a recursive function that will take the first digit of the string and multiply it with the appropriate power of 10 and then add the recursive result for the substring starting at the second index. The termination condition will be when the passed string consists of a single digit. In that case, return the digit represented by the string.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include<bits/stdc++.h>using namespace std; // Recursive function to convert// string to integerint stringToInt(string str){     // If the number represented as a string    // contains only a single digit    // then returns its value    if (str.length() == 1)        return (str[0] - '0');     // Recursive call for the sub-string    // starting at the second character    double y = stringToInt(str.substr(1));     // First digit of the number    double x = str[0] - '0';     // First digit multiplied by the    // appropriate power of 10 and then    // add the recursive result    // For example, xy = ((x * 10) + y)    x = x * pow(10, str.length() - 1) + y;    return int(x);} // Driver codeint main(){    string str = "1235";    cout << (stringToInt(str)) << endl;} // This code is contributed by// Surendra_Gangwar
// Java implementation of the approachpublic class GFG {     // Recursive function to convert string to integer    static int stringToInt(String str)    {         // If the number represented as a string        // contains only a single digit        // then returns its value        if (str.length() == 1)            return (str.charAt(0) - '0');         // Recursive call for the sub-string        // starting at the second character        double y = stringToInt(str.substring(1));         // First digit of the number        double x = str.charAt(0) - '0';         // First digit multiplied by the        // appropriate power of 10 and then        // add the recursive result        // For example, xy = ((x * 10) + y)        x = x * Math.pow(10, str.length() - 1) + y;        return (int)(x);    }     // Driver code    public static void main(String[] args)    {        String str = "1235";        System.out.print(stringToInt(str));    }}
# Python3 implementation of the approach # Recursive function to convert# string to integer  def stringToInt(str):     # If the number represented as a string    # contains only a single digit    # then returns its value    if (len(str) == 1):        return ord(str[0]) - ord('0')     # Recursive call for the sub-string    # starting at the second character    y = stringToInt(str[1:])     # First digit of the number    x = ord(str[0]) - ord('0')     # First digit multiplied by the    # appropriate power of 10 and then    # add the recursive result    # For example, xy = ((x * 10) + y)    x = x * (10**(len(str) - 1)) + y    return x  # Driver codestr = "1235"print(stringToInt(str)) # This code is contributed by PrinciRaj1992
// C# implementation of the approachusing System; class GFG{     // Recursive function to convert string to integer    static int stringToInt(String str)    {         // If the number represented as a string        // contains only a single digit        // then returns its value        if (str.Length == 1)            return (str[0] - '0');         // Recursive call for the sub-string        // starting at the second character        double y = stringToInt(str.Substring(1));         // First digit of the number        double x = str[0] - '0';         // First digit multiplied by the        // appropriate power of 10 and then        // add the recursive result        // For example, xy = ((x * 10) + y)        x = x * Math.Pow(10, str.Length - 1) + y;        return (int)(x);    }     // Driver code    public static void Main(String[] args)    {        String str = "1235";        Console.Write(stringToInt(str));    }} // This code is contributed by Princi Singh
<script> // javascript implementation of the approach        // Recursive function to convert string to integer    function stringToInt(str)    {           // If the number represented as a string        // contains only a single digit        // then returns its value                 if (str.length == 1)            return (str[0] - '0');           // Recursive call for the sub-string        // starting at the second character        var y = stringToInt(str.substring(1));           // First digit of the number        var x = str[0] - '0';           // First digit multiplied by the        // appropriate power of 10 and then        // add the recursive result        // For example, xy = ((x * 10) + y)        x = x * Math.pow(10, str.Length - 1) + y;                 return (x);    }       // Driver code         var str = "1235".split()        document.write(stringToInt(str));       </script>
1235
Time Complexity: O(n)
Auxiliary Space: O(n)
princi singh
SURENDRA_GANGWAR
princiraj1992
VijayRajR1
bunnyram19
devadarsh7
subhammahato348
Mathematical
Recursion
Strings
Strings
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction
Print all subsequences of a string