Convert a String to an Integer using Recursion | Java implementation of the approach ; 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
public class GFG { static int stringToInt ( String str ) { if ( str . length ( ) == 1 ) return ( str . charAt ( 0 ) - '0' ) ; double y = stringToInt ( str . substring ( 1 ) ) ; double x = str . charAt ( 0 ) - '0' ; x = x * Math . pow ( 10 , str . length ( ) - 1 ) + y ; return ( int ) ( x ) ; } public static void main ( String [ ] args ) { String str = "1235" ; System . out . print ( stringToInt ( str ) ) ; } }