-
Notifications
You must be signed in to change notification settings - Fork 0
/
strtoint.c
31 lines (28 loc) · 834 Bytes
/
strtoint.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
// Created by mipsc on 16.06.19.
#include "strtoint.h"
int strtoint(const char* str) {
// Converting a string to int depending on the position of the digit in the
// number. Knows the position of the digit by multiplying by many or one
// 10(s). See strtointTest.c
int count1 = 1;
int count2 = 0;
int digit = 0;
int result = 0;
while (*str) {
count1 *= 10; // Accumulating 10s for shifting
str++;
count2++; // Storing how many steps were walken to reset *str later.
}
// Resetting *str.
str -= count2;
// Depending on the position of the digit (the very first one has the more
// 10s)
while (*str) {
digit = *str - '0';
digit *= count1;
result += digit;
count1 /= 10;
str++;
}
return result / 10;
}