text: Convert a String to an Integer using Recursion | C ++ 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int stringToInt ( string str ) { if ( str . length ( ) == 1 ) return ( str [ 0 ] - '0' ) ; double y = stringToInt ( str . substr ( 1 ) ) ; double x = str [ 0 ] - '0' ; x = x * pow ( 10 , str . length ( ) - 1 ) + y ; return int ( x ) ; } int main ( ) { string str = "1235" ; cout << ( stringToInt ( str ) ) << endl ; }