The atoi() function takes a string (which represents an integer) as an argument and returns its value.
We have discussed iterative implementation of atoi(). How to compute recursively?
We strongly recommend you to minimize your browser and try this yourself first
The idea is to separate the last digit, recursively compute the result for remaining n-1 digits, multiply the result with 10 and add the obtained value to the last digit.
Below is the implementation of the idea.
C++
// Recursive C program to compute atoi() #include <stdio.h> #include <string.h> // Recursive function to compute atoi() int myAtoiRecursive(char *str, int n) { // Base case (Only one digit) if (n == 1) return *str - '0'; // If more than 1 digits, recur for (n-1), multiplu result with 10 // and add last digit return (10 * myAtoiRecursive(str, n - 1) + str[n-1] - '0'); } // Driver Program int main(void) { char str[] = "112"; int n = strlen(str); printf("%d", myAtoiRecursive(str, n)); return 0; } |
Java
// Recursive Java program to compute atoi() class GFG{ // Recursive function to compute atoi() static int myAtoiRecursive(String str, int n) { // Base case (Only one digit) if (n == 1) { return str.charAt(0) - '0'; } // If more than 1 digits, recur for (n-1), // multiplu result with 10 and add last digit return (10 * myAtoiRecursive(str, n - 1) + str.charAt(n - 1) - '0'); } // Driver code public static void main(String[] s) { String str = "112"; int n = str.length(); System.out.println(myAtoiRecursive(str, n)); } } // This code is contributed by rutvik_56 |
Python3
# Python3 program to compute atoi() # Recursive function to compute atoi() def myAtoiRecursive(string, num): # base case, we've hit the end of the string, # we just return the last value if len(string) == 1: return int(string) + (num * 10) # add the next string item into our num value num = int(string[0:1]) + (num * 10) # recurse through the rest of the string # and add each letter to num return myAtoiRecursive(string[1:], num) # Driver Code string = "112" print(myAtoiRecursive(string, 0)) # This code is contributed by Frank-Hu-MSFT |
C#
// Recursive C# program to compute atoi() using System; class GFG{ // Recursive function to compute atoi() static int myAtoiRecursive(string str, int n) { // Base case (Only one digit) if (n == 1) { return str[0] - '0'; } // If more than 1 digits, recur for (n-1), // multiplu result with 10 and add last digit return (10 * myAtoiRecursive(str, n - 1) + str[n - 1] - '0'); } // Driver code public static void Main() { string str = "112"; int n = str.Length; Console.Write(myAtoiRecursive(str, n)); } } // This code is contributed by Nidhi_Biet |
Output:
112
This article is contributed by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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:
- Write your own atoi()
- Recursive Functions
- Recursive Insertion Sort
- Recursive function to check if a string is palindrome
- Recursive function to do substring search
- String slicing in Python to check if a string can become empty by recursive deletion
- Program to count vowels in a string (Iterative and Recursive)
- Recursive Programs to find Minimum and Maximum elements of array
- Recursive program to print formula for GCD of n integers
- Recursive solution to count substrings with same first and last characters
- Recursive Tower of Hanoi using 4 pegs / rods
- Recursive function to delete k-th node from linked list
- Recursive insertion and traversal linked list
- Count consonants in a string (Iterative and recursive methods)
- First uppercase letter in a string (Iterative and Recursive)
- Recursive approach for alternating split of Linked List
- Recursive program for prime number
- Function to copy string (Iterative and Recursive)
- Recursive Practice Problems with Solutions
- Check if linked list is sorted (Iterative and Recursive)

