Skip to content

Commit 973e787

Browse files
authored
Convert String To Integer
1 parent a0e95ad commit 973e787

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Hacker Blocks */
2+
/* Title - Convert String to Integer */
3+
/* Created By - Akash Modak */
4+
/* Date - 14/07/2020 */
5+
6+
// Take as input str, a number in form of a string. Write a recursive function to convert the number in string form to number in integer form. E.g. for “1234” return 1234. Print the value returned.
7+
8+
// Input Format
9+
// Enter a number in form of a String
10+
11+
// Constraints
12+
// 1 <= Length of String <= 10
13+
14+
// Output Format
15+
// Print the number after converting it into integer
16+
17+
// Sample Input
18+
// 1234
19+
// Sample Output
20+
// 1234
21+
// Explanation
22+
// Convert the string to int. Do not use any inbuilt functions.
23+
24+
#include<iostream>
25+
#include<cstring>
26+
using namespace std;
27+
int convert(char *a,int n){
28+
if(n==0)
29+
return 0;
30+
int digit = a[n-1]-'0';
31+
int ans = convert(a,n-1);
32+
return ans*10+digit;
33+
}
34+
int main() {
35+
char temp[1000];
36+
cin>>temp;
37+
cout<<convert(temp,strlen(temp));
38+
return 0;
39+
}

0 commit comments

Comments
 (0)