-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-of-written-numbers-to-actual-number.c
125 lines (99 loc) · 2.68 KB
/
string-of-written-numbers-to-actual-number.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
int isNumber(char* str) {
char* numbers[10] = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
for (int i = 0; i < 10; i++) {
if (strcmp(str, numbers[i]) == 0) {
return i;
}
}
return -1;
}
int isSignal(char* str) {
char* signals[] = { "plus", "minus" };
if (strcmp(str, signals[0]) == 0) {
return 1;
} else if (strcmp(str, signals[1]) == 0) {
return -1;
}
return 0;
}
char* buildExtense(char* number) {
char* result = malloc(1 * sizeof(char));
char* numbers[10] = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
if (number[0] == '-') {
result = realloc(result, 9 * sizeof(char));
strcpy(result, "negative");
for (int i = 1; i < strlen(number); i++) {
char* digit = numbers[number[i] - '0'];
result = realloc(result, (strlen(result) + strlen(digit)) * sizeof(char));
strcat(result, digit);
}
} else {
for (int i = 0; i < strlen(number); i++) {
char* digit = numbers[number[i] - '0'];
result = realloc(result, (strlen(result) + strlen(digit)) * sizeof(char));
if (i == 0) {
strcpy(result, digit);
} else {
strcat(result, digit);
}
}
}
return result;
}
void StringChallenge(char * str) {
char* reading = malloc(1 * sizeof(char));
char* temp = malloc(6 * sizeof(char));
int current = 0;
int signal = 1;
while (1) {
if (str[0] == '\0') {
current += (atoi(reading) * signal);
free(reading);
char* result = malloc(strlen(str) * sizeof(char));
sprintf(result, "%d", current);
printf("%s\n", buildExtense(result));
return;
}
for (int i = 3; i <= 5; i++) {
strncpy(temp, str, i);
int num = isNumber(temp);
if (num > -1) {
char res[2];
sprintf(res, "%d", num);
if (strlen(reading) == 0) {
strcpy(reading, res);
} else {
strcat(reading, res);
}
reading = realloc(reading, (strlen(reading) + 1) * sizeof(char));
str += strlen(temp);
free(temp);
temp = malloc(6 * sizeof(char));
break;
}
int sign = isSignal(temp);
if (sign != 0) {
current += (atoi(reading) * signal);
free(reading);
reading = malloc(1 * sizeof(char));
signal = sign;
str += strlen(temp);
free(temp);
temp = malloc(6 * sizeof(char));
break;
}
continue;
}
}
}
int main(void) {
// keep this function call here
StringChallenge(coderbyteInternalStdinFunction(stdin));
return 0;
}