-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathString to Integer.cpp
58 lines (36 loc) · 940 Bytes
/
String to Integer.cpp
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
/*
String to Integer
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.
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
*/
void stringToNumber_Actual(char temp[], int &n) {
// base case
if(*temp == '\0') {
return;
}
//nth step
n = (n * 10) + (*temp - '0') ;
//hyothesis step
stringToNumber_Actual(temp + 1, n);
}
int stringToNumber(char temp[]) {
// Write your code here
int answer = 0;
stringToNumber_Actual(temp, answer);
return answer;
}