-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToInteger.cpp
More file actions
56 lines (42 loc) · 1.04 KB
/
Copy pathStringToInteger.cpp
File metadata and controls
56 lines (42 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Title: String to Integer
Problem statement
Write a recursive function to convert a given string into the number it represents. That is input
will be a numeric string that contains only numbers, you need to convert the string into
corresponding integer and return the answer.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
Numeric string S (string, Eg. "1234")
Output format :
Corresponding integer N (int, Eg. 1234)
Constraints :
0 < |S| <= 9
where |S| represents length of string S.
Sample Input 1 :
00001231
Sample Output 1 :
1231
Sample Input 2 :
12567
Sample Output 2 :
12567
*/
#include<iostream>
#include<math.h>
using namespace std;
int StringToNumber(char input[]) {
if(input[0] == '\0') {
return 0;
}
int smallOutput = StringToNumber(&input[1]);
int length = 0;
while(input[length] != '\0') {
length++;
}
return ((int)input[0]-48) * pow(10, length-1) + smallOutput;
}
int main() {
char input[50];
cin >> input;
cout << StringToNumber(input) << endl;
}