fork(2) 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", "Lam", "Sau", "Bay", "Tam", "Chin"};
  8. char base[][10] = {"", "Muoi", "Tram", "Nghin", "Muoi", "Tram", "Trieu", "Muoi", "Tram", "Ty"};
  9.  
  10. char *d2w(char s[]) {
  11. char *word = calloc(1, MS);
  12. int size = strlen(s);
  13. if (size == 0)
  14. return NULL;
  15. for (int i = 0; i < size; i++)
  16. s[i] -= '0';
  17. if (size == 1) {
  18. strcpy(word, oneDigit[(int)s[0]]);
  19. return word;
  20. }
  21. for (int i = 0, j = size - 1; i < j; i++, j--) {
  22. char t = s[i];
  23. s[i] = s[j];
  24. s[j] = t;
  25. }
  26. for (int i = size - 1; i >= 0; i--) {
  27. int k = i;
  28. while (k >= 10) {
  29. k -= 9;
  30. }
  31. if (!strcmp(oneDigit[(int)s[i]], "Khong") && !strcmp(base[k], "Muoi") && s[i - 1] != 0)
  32. strcat(word, "Linh ");
  33. else if (!strcmp(oneDigit[(int)s[i]], "Mot") && !strcmp(base[k], "Muoi"))
  34. strcat(word, "Muoi ");
  35. else if (s[i] != 0 || (!strcmp(base[k], "Tram") && s[i - 2] != 0)) {
  36. strcat(word, oneDigit[(int)s[i]]);
  37. strcat(word, " ");
  38. strcat(word, base[k]);
  39. strcat(word, " ");
  40. } else if (((i % 3 == 0) && (s[i + 1] != 0 || s[i + 2] != 0)) || (!strcmp(base[k - 6], "Nghin"))) {
  41. strcat(word, base[k]);
  42. strcat(word, " ");
  43. }
  44. }
  45. return word;
  46. }
  47.  
  48. int main() {
  49. char s[MS];
  50. do {
  51. printf("Enter an integer: ");
  52. scanf("%s", s);
  53. while (getchar() != '\n');
  54. for (int i = 0; i < strlen(s); i++)
  55. printf("%c", s[i]);
  56. printf(" in words is: %s\n", d2w(s));
  57. printf("\nDo you want continue:\n");
  58. printf("1. Yes\n0. No\n");
  59. } while (getchar() != '0');
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 9432KB
stdin
1000001000000001
1
15
0
stdout
Enter an integer: 1000001000000001 in words is: Mot Trieu Khong Tram Linh Mot Ty Khong Tram Linh Mot  

Do you want continue:
1. Yes
0. No
Enter an integer: 15 in words is: Muoi Lam  

Do you want continue:
1. Yes
0. No