Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #define N_WORDS 60
- char dictionary2[N_WORDS][30];
- char answer[N_WORDS+1][30];
- char dictionary[N_WORDS][30]={
- "a",
- "all",
- "an",
- "and",
- "are",
- "as",
- "at",
- "ate",
- "be",
- "but",
- "by",
- "can",
- "each",
- "eat",
- "era",
- "ew",
- "for",
- "from",
- "had",
- "have",
- "he",
- "his",
- "hot",
- "how",
- "I",
- "in",
- "is",
- "it",
- "Neo",
- "No",
- "of",
- "on",
- "one",
- "or",
- "other",
- "out",
- "said",
- "she",
- "some",
- "tea",
- "that",
- "The",
- "theory",
- "there",
- "they",
- "this",
- "To",
- "tub",
- "up",
- "use",
- "was",
- "we",
- "were",
- "what",
- "when",
- "Who",
- "with",
- "word",
- "you",
- "your"
- };
- /* #define ONLINE_JUDGE */
- void count_letters(char *input_str, int *counts);
- int equal_counts(int *counts1, int *counts2);
- void find_words(char *input);
- int main(void)
- {
- /*
- #ifndef ONLINE_JUDGE
- freopen("ps5_3_test.txt", "r", stdin);
- #endif
- */
- char input[30];
- while (scanf("%29s", input)!=EOF) {
- find_words(input);
- }
- return 0;
- }
- void find_words(char *input)
- {
- int counts1[26]={0}, counts2[26]={0};
- int found = 0;
- int i, j;
- int p=strlen(input);
- input[p]='\0';
- count_letters(input, counts1);
- for (i=0;i<N_WORDS;i++){
- for (j=0;;j++){
- if(j==strlen(dictionary[i]))
- break;
- dictionary2[i][j]=tolower(dictionary[i][j]);
- }
- count_letters(dictionary2, counts2);
- if (equal_counts(counts1, counts2)==1){
- found++;
- for (j=0;;j++){
- if (j==strlen(dictionary2[i]))
- break;
- answer[found][j]=dictionary[i][j];
- }
- }
- }
- for (j=1;j<found;j++)
- printf("%s,", answer[j]);
- if (found) {
- printf("%s\n", answer[found]);
- } else {
- printf("None.\n");
- }
- }
- /*
- Count the number of occurrences of each letter from 'a' to 'z'.
- Store the counts in the integer array counts[].
- The integer array counts[] has 26 elements. Each element should
- record the number of occurrences of the corresponding letter.
- counts[0] for 'a', counts[1] for 'b', ..., etc.
- */
- void count_letters(char *input_str, int *counts)
- {
- int i;
- for (i=0;;i++){
- if (input_str[i]=='\0')
- break;
- counts[input_str[i]-'a']++;
- }
- }
- /*
- Return 1 if the two arrays counts1[] and counts2[] contain
- exactly the same values.
- Otherwise, return 0.
- */
- int equal_counts(int *counts1, int *counts2)
- {
- int i;
- for (i=0;i<26;i++){
- if (counts1[i] != counts2[i])
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement