-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell.c
76 lines (63 loc) · 2.31 KB
/
spell.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
#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);
char digit [21][10] = { "", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"};
char tens [11][10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
char str[1000] = {0};
int prev=0, div=1000;
strcpy(str, "");
while(div) {
if ((num / div) % 10 > 0 || (div == 10 && (num%100) > 0)) {
if (prev) {
strcat(str, " and");
prev = 0;
}
switch(div) {
case 1000:
if (strlen(str) > 0 && str[strlen(str) - 1] != ' ')
strcat(str, " ");
strcat(str, digit[(num / div) % 10]);
if (((num / div) % 10) > 1)
strcat(str, " thousands");
else
strcat(str, " thousand");
prev = 1;
break;
case 100:
if (strlen(str) > 0 && str[strlen(str) - 1] != ' ')
strcat(str, " ");
strcat(str, digit[(num / div) % 10]);
if (((num / div) % 10) > 1)
strcat(str, " hundreds");
else
strcat(str, " hundred");
prev = 1;
break;
case 10:
if ( (num%100) >= 10 && (num%100) <= 19)
{
if (strlen(str) > 0 && str[strlen(str) - 1] != ' ')
strcat(str, " ");
strcat(str, digit[num%100]);
}
else {
if (strlen(str) > 0 && str[strlen(str) - 1] != ' ')
strcat(str, " ");
strcat(str, tens[(num%100)/10]);
if (strlen(str) > 0 && str[strlen(str) - 1] != ' ')
strcat(str, " ");
strcat(str, digit[num%10]);
}
break;
}
}
div /= 10;
}
printf("%d %s\n", num, str);
}