Take as input str, a number in form of a string. Write a recursive function to convert the number in string form to number in integer form. E.g. for “1234” return 1234. Print the value returned.
how to approach this question with recursion?
Convert string to integer using recursion
Hi priyanshi,to solve this question recursively you can make a function with two parametres one is the string and second is the int(let it be int ans). Everytime you will pass str.substring(1) in the string part and 10*ans+str.charAt(0)-48 in the integer part and do this work until you get str.length=0.When you get str.length()==0 then return that ans.We have done -48 so that we get that particular integer and not its ascii value and we multiply it with 10 so as to form the number.
Also once try to dry run it before implementing the logic.
2 Likes
i wasn’t able to frame the integer argument. Thank you