Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define N_WORDS 60
  5.  
  6. char dictionary2[N_WORDS][30];
  7. char answer[N_WORDS+1][30];
  8. char dictionary[N_WORDS][30]={
  9. "a",
  10. "all",
  11. "an",
  12. "and",
  13. "are",
  14. "as",
  15. "at",
  16. "ate",
  17. "be",
  18. "but",
  19. "by",
  20. "can",
  21. "each",
  22. "eat",
  23. "era",
  24. "ew",
  25. "for",
  26. "from",
  27. "had",
  28. "have",
  29. "he",
  30. "his",
  31. "hot",
  32. "how",
  33. "I",
  34. "in",
  35. "is",
  36. "it",
  37. "Neo",
  38. "No",
  39. "of",
  40. "on",
  41. "one",
  42. "or",
  43. "other",
  44. "out",
  45. "said",
  46. "she",
  47. "some",
  48. "tea",
  49. "that",
  50. "The",
  51. "theory",
  52. "there",
  53. "they",
  54. "this",
  55. "To",
  56. "tub",
  57. "up",
  58. "use",
  59. "was",
  60. "we",
  61. "were",
  62. "what",
  63. "when",
  64. "Who",
  65. "with",
  66. "word",
  67. "you",
  68. "your"
  69. };
  70. /* #define ONLINE_JUDGE */
  71.  
  72. void count_letters(char *input_str, int *counts);
  73. int equal_counts(int *counts1, int *counts2);
  74. void find_words(char *input);
  75.  
  76. int main(void)
  77. {
  78. /*
  79. #ifndef ONLINE_JUDGE
  80. freopen("ps5_3_test.txt", "r", stdin);
  81. #endif
  82. */
  83. char input[30];
  84.  
  85. while (scanf("%29s", input)!=EOF) {
  86. find_words(input);
  87. }
  88.  
  89. return 0;
  90. }
  91.  
  92. void find_words(char *input)
  93. {
  94. int counts1[26]={0}, counts2[26]={0};
  95. int found = 0;
  96. int i, j;
  97. int p=strlen(input);
  98.  
  99. input[p]='\0';
  100.  
  101. count_letters(input, counts1);
  102.  
  103. for (i=0;i<N_WORDS;i++){
  104.  
  105. for (j=0;;j++){
  106. if(j==strlen(dictionary[i]))
  107. break;
  108. dictionary2[i][j]=tolower(dictionary[i][j]);
  109. }
  110. count_letters(dictionary2, counts2);
  111.  
  112.  
  113. if (equal_counts(counts1, counts2)==1){
  114. found++;
  115. for (j=0;;j++){
  116. if (j==strlen(dictionary2[i]))
  117. break;
  118. answer[found][j]=dictionary[i][j];
  119. }
  120.  
  121. }
  122. }
  123.  
  124. for (j=1;j<found;j++)
  125. printf("%s,", answer[j]);
  126.  
  127. if (found) {
  128. printf("%s\n", answer[found]);
  129. } else {
  130. printf("None.\n");
  131. }
  132. }
  133. /*
  134. Count the number of occurrences of each letter from 'a' to 'z'.
  135. Store the counts in the integer array counts[].
  136. The integer array counts[] has 26 elements. Each element should
  137. record the number of occurrences of the corresponding letter.
  138. counts[0] for 'a', counts[1] for 'b', ..., etc.
  139. */
  140. void count_letters(char *input_str, int *counts)
  141. {
  142. int i;
  143.  
  144. for (i=0;;i++){
  145. if (input_str[i]=='\0')
  146. break;
  147. counts[input_str[i]-'a']++;
  148.  
  149.  
  150. }
  151. }
  152.  
  153. /*
  154. Return 1 if the two arrays counts1[] and counts2[] contain
  155. exactly the same values.
  156. Otherwise, return 0.
  157. */
  158. int equal_counts(int *counts1, int *counts2)
  159. {
  160. int i;
  161.  
  162. for (i=0;i<26;i++){
  163. if (counts1[i] != counts2[i])
  164. return 0;
  165.  
  166. }
  167.  
  168. return 1;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement