Created
April 13, 2021 05:25
-
-
Save Sustainability4/b2610bed9f8a83bf21b86164eb5990ad to your computer and use it in GitHub Desktop.
String to integers using recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
#include <cmath> | |
int number(char input[], int size){ | |
if (size < 1){ | |
return 0; | |
} | |
int sum = number(input+1, size-1); | |
int i = input[0]; | |
i = i-48; | |
return sum + i*pow(10,size-1); | |
} | |
int size(char input[]){ | |
int i = 0; | |
while(input[i]){ | |
i++; | |
} | |
return i ; | |
} | |
int stringToNumber(char input[]) { | |
int length = size(input); | |
int sum = number(input, length); | |
return sum ; | |
} | |
int main() { | |
char input[50]; | |
cin >> input; | |
cout << stringToNumber(input) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment