blob: 3bd1dfb317e5aa7609795420db5dcfa623c53cfd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*
Challenge 004
Challenge #2
You are given a file containing a list of words (case insensitive 1 word per
line) and a list of letters. Print each word from the file that can be made
using only letters from the list. You can use each letter only once (though
there can be duplicates and you can use each of them once), you don’t have to
use all the letters. (Disclaimer: The challenge was proposed by Scimon Proctor)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD 1024
void strtolower(char* str) {
for (char* p = str; *p; p++)
*p = tolower(*p);
}
bool strisalpha(const char* str) {
for (const char* p = str; *p; p++)
if (!isalpha(*p))
return false;
return true;
}
void trim(char* str) {
int len = strlen(str);
while (len > 0 && isspace(str[len-1]))
str[--len] = '\0';
}
bool matches(const char* word, const char* letters) {
char pending[MAX_WORD];
strcpy(pending, word);
for (const char* p = letters; *p; p++) {
char* found = strchr(pending, *p);
if (found) *found = ' ';
}
for (char* p = pending; *p; p++)
if (*p != ' ') return false;
return true;
}
int main(int argc, char* argv[]) {
if (argc != 2) return EXIT_FAILURE;
char letters[MAX_WORD];
strcpy(letters, argv[1]);
strtolower(letters);
FILE* fp = fopen("words.txt", "r");
if (!fp) return EXIT_FAILURE;
char word[MAX_WORD];
while (fgets(word,sizeof(word),fp)) {
trim(word);
if (strlen(word) >= 2 &&
strisalpha(word) &&
matches(word, letters)
)
printf("%s\n", word);
}
fclose(fp);
return EXIT_SUCCESS;
}
|