fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MS 1000
  6.  
  7. char oneDigit[][10] = {"Khong", "Mot", "Hai", "Ba", "Bon", "Nam", "Sau", "Bay", "Tam", "Chin"};
  8. char base[][10] = {"", "Muoi", "Tram", "Nghin", "Muoi", "Tram", "Trieu", "Muoi", "Tram", "Ty"};
  9.  
  10. int checkString(char s[]) {
  11. for (int i = 0; i < strlen(s); i++)
  12. if (s[i] < '0' || s[i] > '9')
  13. return 0;
  14. return 1;
  15. }
  16.  
  17. char *preWord(const char s[]) {
  18. static char word[10] = {0};
  19. int size = strlen(s);
  20. int i = size - 1, j = 0;
  21. while (s[i--] != ' ');
  22. while (s[i--] != ' ');
  23. i += 2;
  24. while(s[i] != ' ' && s[i] != '\0') {
  25. word[j++] = s[i++];
  26. }
  27. word[j] = '\0';
  28. return word;
  29. }
  30.  
  31. char *d2w(char s[]) {
  32. char *word = calloc(MS, 1);
  33. int size = strlen(s);
  34. if (size == 0 || !checkString(s))
  35. return NULL;
  36. for (int i = 0; i < size; i++)
  37. s[i] -= '0';
  38. if (size == 1) {
  39. strcpy(word, oneDigit[(int)s[0]]);
  40. return word;
  41. }
  42. for (int i = 0, j = size - 1; i < j; i++, j--) {
  43. char t = s[i];
  44. s[i] = s[j];
  45. s[j] = t;
  46. }
  47. while (s[size - 1] == 0) {
  48. size--;
  49. }
  50. for (int i = size - 1; i >= 0; i--) {
  51. int k = i;
  52. while (k >= 10) {
  53. k -= 9;
  54. }
  55. if (!strcmp(oneDigit[(int)s[i]], "Khong") && !strcmp(base[k], "Muoi") && s[i - 1] != 0)
  56. strcat(word, "Linh ");
  57. else if (!strcmp(oneDigit[(int)s[i]], "Mot") && !strcmp(base[k], "Muoi"))
  58. strcat(word, "Muoi ");
  59. else if (s[i] != 0 || (!strcmp(base[k], "Tram") && (s[i - 2] != 0 || s[i - 1] != 0))) {
  60. if (s[i] == 5 && ((i == 0 || s[i + 1] == 1)) && strcmp(preWord(word), "Linh")) {
  61. strcat(word, "Lam");
  62. strcat(word, " ");
  63. } else {
  64. strcat(word, oneDigit[(int)s[i]]);
  65. strcat(word, " ");
  66. }
  67. strcat(word, base[k]);
  68. strcat(word, " ");
  69. } else if (((i % 3 == 0) && (s[i + 1] != 0 || s[i + 2] != 0)) || (!strcmp(base[k - 6], "Nghin"))) {
  70. strcat(word, base[k]);
  71. strcat(word, " ");
  72. }
  73. }
  74. return word;
  75. }
  76.  
  77. int main() {
  78. char s[MS];
  79. while (fscanf(stdin, "%s", s) != EOF) {
  80. int k = 0;
  81. while (s[k] == '0') {
  82. k++;
  83. }
  84. for (int i = k; i < strlen(s); i++)
  85. printf("%c", s[i]);
  86. printf(" in words is: %s\n", d2w(s));
  87. }
  88. return 0;
  89. }
  90.  
Success #stdin #stdout 0s 9432KB
stdin
1000005
1010000
15
125
120
110
115
105
2001
1234567
5005105
5015110
2010000
2010
5555555
1050
5
25
1054
001
sda
1000001011111
1000001000000001
015120
stdout
1000005 in words is: Mot Trieu Khong Tram Linh Nam  
1010000 in words is: Mot Trieu Khong Tram Muoi Nghin 
15 in words is: Muoi Lam  
125 in words is: Mot Tram Hai Muoi Lam  
120 in words is: Mot Tram Hai Muoi  
110 in words is: Mot Tram Muoi  
115 in words is: Mot Tram Muoi Lam  
105 in words is: Mot Tram Linh Nam  
2001 in words is: Hai Nghin Khong Tram Linh Mot  
1234567 in words is: Mot Trieu Hai Tram Ba Muoi Bon Nghin Nam Tram Sau Muoi Bay  
5005105 in words is: Nam Trieu Khong Tram Linh Nam Nghin Mot Tram Linh Nam  
5015110 in words is: Nam Trieu Khong Tram Muoi Lam Nghin Mot Tram Muoi  
2010000 in words is: Hai Trieu Khong Tram Muoi Nghin 
2010 in words is: Hai Nghin Khong Tram Muoi  
5555555 in words is: Nam Trieu Nam Tram Nam Muoi Nam Nghin Nam Tram Nam Muoi Lam  
1050 in words is: Mot Nghin Khong Tram Nam Muoi  
5 in words is: Nam
25 in words is: Hai Muoi Lam  
1054 in words is: Mot Nghin Khong Tram Nam Muoi Bon  
1 in words is: Mot  
sda in words is: (null)
1000001011111 in words is: Mot Nghin Ty Khong Tram Linh Mot Trieu Khong Tram Muoi Mot Nghin Mot Tram Muoi Mot  
1000001000000001 in words is: Mot Trieu Khong Tram Linh Mot Ty Khong Tram Linh Mot  
15120 in words is: Muoi Lam Nghin Mot Tram Hai Muoi