Convert a String to an Integer using Recursion | Recursive function to convert string to integer ; If the number represented as a string contains only a single digit then returns its value ; Recursive call for the sub - string starting at the second character ; First digit of the number ; First digit multiplied by the appropriate power of 10 and then add the recursive result For example , xy = ( ( x * 10 ) + y ) ; Driver code
def stringToInt ( str ) : NEW_LINE INDENT if ( len ( str ) == 1 ) : NEW_LINE INDENT return ord ( str [ 0 ] ) - ord ( '0' ) ; NEW_LINE DEDENT y = stringToInt ( str [ 1 : ] ) ; NEW_LINE x = ord ( str [ 0 ] ) - ord ( '0' ) ; NEW_LINE x = x * ( 10 ** ( len ( str ) - 1 ) ) + y ; NEW_LINE return int ( x ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "1235" ; NEW_LINE print ( stringToInt ( str ) ) ; NEW_LINE DEDENT