-
Notifications
You must be signed in to change notification settings - Fork 0
/
atoi.c
49 lines (36 loc) · 942 Bytes
/
atoi.c
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
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int convert(string input);
int main(void)
{
string input = get_string("Enter a positive integer: ");
for (int i = 0, n = strlen(input); i < n; i++)
{
if (!isdigit(input[i]))
{
printf("Invalid Input!\n");
return 1;
}
}
// Convert string to int
printf("%i\n", convert(input));
}
int convert(string input)
{
// TODO
int length=strlen(input);
//base case recursive
if (length==1)
{
return input[0]-'0'; // ascii code 0=48, so -0 to get the number's ascii code
}
// recursive base
char last_digit= input [length -1];
int coverted_last_digit= last_digit - '0';
input[length-1]='\0';
//Return this value plus 10 times the integer value of the new shortened string
return coverted_last_digit + 10* convert(input);
}