1. #include <stdio.h>
2. #include <string.h>
3. #include <stdlib.h>
4.
5. char *numberArray[] = {" Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen"," Sixteen", " Seventeen", " Eighteen", "Nineteen"};
6.
7.
8. char *tensArray[] = {"",""," Twenty"," Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety"};
9.
10. char *middNames[] = {""," Thousand"," Million"," Billion"};
11.
12. char* Numbertowords(int temp)
13. {
14.
15. /*Initialize a string */
16. char *textToTransfer;
17.
18. int hund_,ten_,unit_,saved_tens;
19.
20. /* I expect maximum 100 characters per 3 digits as of now.Try to make it dynamic */
21. textToTransfer = (char*)calloc(1,100);
22.
23. hund_ = temp / 100;
24. ten_ = temp % 100;
25. saved_tens = ten_;
26. if (hund_)
27. {
28.
29. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
30. strcat(textToTransfer,numberArray[hund_]);
31. strcat(textToTransfer," hundred");
32. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
33.
34.
35. if (ten_)
36. {
37. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
38. strcat(textToTransfer," and");
39. }
40. }
41. ten_ = ten_ / 10;
42. unit_ = saved_tens % 10;
43.
44. if (ten_ > 1)
45. {
46. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
47. strcat(textToTransfer,tensArray[ten_]);
48. if (unit_)
49. {
50. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
51. strcat(textToTransfer,numberArray[unit_]);
52. }
53. }
54. else if(saved_tens != 0)
55. {
56. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
57. strcat(textToTransfer,numberArray[saved_tens]);
58. }
59. printf("%d->%s()::%s\n",__LINE__,__func__,textToTransfer);
60. return (textToTransfer);
61. }
62.
63.
64. int main()
65. {
66. int number,dummy;
67. char neg_flag = 0;
68. char *strings[10];
69. int i,j;
70.
71. char fullstring[150] = {'\0'}; /* try to make it dynamic */
72. number = 0;
73. printf("\nEnter the number you want to convert to words:->");
74. scanf("%d",&number);
75. if ( 0 > number)
76. neg_flag = 1;
77.
78. /* store in a dummy variable */
79. dummy = number;
80.
81. /* Conditions starts */
82. if (0 == number)
83. {
84. printf("\nZero");
85. exit(0);
86. }
87. else
88. {
89. /* Convert to three digits groups */
90. /* a 7 Digit number contains only 3 3digit groups*/
91. for (i=0; ((i <= 2) && (dummy != 0));i++)
92. {
93. strings[i] = Numbertowords(dummy % 1000);
94. dummy /= 1000;
95. printf("i = %d Dummy = %d\n",i,dummy);
96. }
97. j = i;
98. /* First print the strings */
99. for(i = 0;i < j;i++)
100. {
101. printf("%s\t",strings[i]);
102. }
103. }
104. /* Now we have the strings in the strings[i] array */
105. for (i = (j-1); i >=0; i--)
106. {
107. strcat(fullstring,strings[i]);
108. strcat(fullstring,middNames[i]);
109. }
110. printf("\n*********************************\n");
111. printf("\n Your input : %d\n",number);
112. printf("\n Mystring : ");
113. if (neg_flag != 0)
114. printf("Negative");
115. printf(" %s",fullstring);
116. printf("\n*********************************\n");
117.
118. return 0;
119. }
No comments:
Post a Comment