Recursive Implementation of atoi (String to Integer)
Get a Strong Hold DSA practice problem on Onlearn.
Difficulty: hard.
Topics: Recursive Implementation of atoi(), Recursion, String Manipulation, Time Complexity, Space Complexity, Data Types, Conditional Statements, Input/Output Operations, edge cases, string processing, number manipulation, recursion, number conversion, Number Manipulation.
Problem Statement Implement the atoi function, which converts a string to an integer. The function should behave as follows: 1. Read and discard leading whitespace : The function should first discard as many whitespace characters (only ' ') as necessary until the first non whitespace character is found. 2. Determine sign : Then, from this character, take an optional initial plus sign + or minus sign . If present, this sign will determine the sign of the final result. If not, the result is positive. 3. Read digits : Next, as many numerical digits (0 9) as possible are read until a non digit character or the end of the input string is reached. 4. Convert to integer : The digits are then converted into an integer. 5. Handle non digit characters : If the first non whitespace character is not an optional sign or digit, no valid conversion could be performed. In this case, or if no digits were read, return 0. 6. Handle overflow : If the integer is out of the 32 bit signed integer range [ 2^31, 2^31 1], then return INT MAX (2147483647) if the number is positive, or INT MIN ( 2147483648) if the number is negative. Implement this function using a recursive approach. Input Specification A single string s. Output Specification The integer result of the conversion. Constraints 0 <= s.length <= 200 s consists of English letters (lower case and upper case), digits (0 9), ' ', '+', ' ', and '.'. Sample Test Cases Sample Input 1: Sample Output 1: Sample Input 2: Sample Output 2: Sample Input 3: Sample Output 3: Sample Input 4: Sample Output 4: Sample Input 5: Sample Output 5: