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++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Recursive function to convert // string to integer 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.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 code int main() { string str = "1235"; cout << (stringToInt(str)) << endl; } // This code is contributed by // Surendra_Gangwar |
Java
// Java implementation of the approach public 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
# 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 int(x); # Driver code if __name__ == '__main__': str = "1235"; print(stringToInt(str)); # This code is contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using 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 |
1235
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Digital Root of a given large integer using Recursion
- Convert string to integer without using any in-built functions
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Convert a String to Integer Array in C/C++
- Minimum decrements to make integer A divisible by integer B
- Print reverse of a string using recursion
- Product of nodes at k-th level in a tree represented as string using Recursion
- Program for length of a string using recursion
- Move all occurence of letter 'x' from the string s to the end using Recursion
- Decimal to Binary using recursion and without using power operator
- Convert given integer X to the form 2^N - 1
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Length of longest palindromic sub-string : Recursion
- Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion
- Remove duplicates from a sorted linked list using recursion
- Reverse a Doubly linked list using recursion
- Sum of natural numbers using recursion
- Decimal to binary number using recursion
- Sum of digit of a number using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

