Convert string to integer without using any in | C ++ program for the above approach ; Function to convert string to integer without using functions ; Initialize a variable ; Iterate till length of the string ; Subtract 48 from the current digit ; Print the answer ; Driver Code ; Given string of number ; Function Call
#include <iostream> NEW_LINE using namespace std ; void convert ( string s ) { int num = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + ( int ( s [ i ] ) - 48 ) ; cout << num ; } int main ( ) { char s [ ] = "123" ; convert ( s ) ; return 0 ; }