Skip to content

Instantly share code, notes, and snippets.

@Sustainability4
Created April 13, 2021 05:25
Show Gist options
  • Save Sustainability4/b2610bed9f8a83bf21b86164eb5990ad to your computer and use it in GitHub Desktop.
Save Sustainability4/b2610bed9f8a83bf21b86164eb5990ad to your computer and use it in GitHub Desktop.
String to integers using recursion
#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