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