Skip to content

Commit b712432

Browse files
committed
addedcodeforgeneralquestion
Signed-off-by: Harsh kumar <68609382+harsh6768-svg@users.noreply.github.com>
1 parent 127f098 commit b712432

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* C program to print a given number in words. The program
2+
handles numbers from 0 to 9999
3+
Just take an example if the user privide the number
4+
1234 -Input
5+
One thousand Two hundred and twenty four -Output
6+
7+
8+
9+
10+
*/
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
15+
/* A function that prints given number in words */
16+
void convert_to_words(char* num)
17+
{
18+
int len = strlen(
19+
num); // Get number of digits in given number
20+
21+
/* Base cases */
22+
if (len == 0) {
23+
fprintf(stderr, "empty string\n");
24+
return;
25+
}
26+
if (len > 4) {
27+
fprintf(stderr,
28+
"Length more than 4 is not supported\n");
29+
return;
30+
}
31+
32+
/* The first string is not used, it is to make
33+
array indexing simple */
34+
char* single_digits[]
35+
= { "zero", "one", "two", "three", "four",
36+
"five", "six", "seven", "eight", "nine" };
37+
38+
/* The first string is not used, it is to make
39+
array indexing simple */
40+
char* two_digits[]
41+
= { "", "ten", "eleven", "twelve",
42+
"thirteen", "fourteen", "fifteen", "sixteen",
43+
"seventeen", "eighteen", "nineteen" };
44+
45+
/* The first two string are not used, they are to make
46+
array indexing simple*/
47+
char* tens_multiple[] = { "", "", "twenty",
48+
"thirty", "forty", "fifty",
49+
"sixty", "seventy", "eighty",
50+
"ninety" };
51+
52+
char* tens_power[] = { "hundred", "thousand" };
53+
54+
/* Used for debugging purpose only */
55+
printf("\n%s: ", num);
56+
57+
/* For single digit number */
58+
if (len == 1) {
59+
printf("%s\n", single_digits[*num - '0']);
60+
return;
61+
}
62+
63+
/* Iterate while num is not '\0' */
64+
while (*num != '\0') {
65+
66+
/* Code path for first 2 digits */
67+
if (len >= 3) {
68+
if (*num - '0' != 0) {
69+
printf("%s ", single_digits[*num - '0']);
70+
printf("%s ",
71+
tens_power[len - 3]); // here len can
72+
// be 3 or 4
73+
}
74+
--len;
75+
}
76+
77+
/* Code path for last 2 digits */
78+
else {
79+
/* Need to explicitly handle 10-19. Sum of the
80+
two digits is used as index of "two_digits"
81+
array of strings */
82+
if (*num == '1') {
83+
int sum = *num - '0' + *(num + 1) - '0';
84+
printf("%s\n", two_digits[sum]);
85+
return;
86+
}
87+
88+
/* Need to explicitely handle 20 */
89+
else if (*num == '2' && *(num + 1) == '0') {
90+
printf("twenty\n");
91+
return;
92+
}
93+
94+
/* Rest of the two digit numbers i.e., 21 to 99
95+
*/
96+
else {
97+
int i = *num - '0';
98+
printf("%s ", i ? tens_multiple[i] : "");
99+
++num;
100+
if (*num != '0')
101+
printf("%s ",
102+
single_digits[*num - '0']);
103+
}
104+
}
105+
++num;
106+
}
107+
}
108+
109+
/* Driver program to test above function */
110+
int main(void)
111+
{
112+
convert_to_words("9923");
113+
convert_to_words("523");
114+
convert_to_words("89");
115+
convert_to_words("8");
116+
117+
return 0;
118+
}

0 commit comments

Comments
 (0)