-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathC.c
37 lines (33 loc) · 1.06 KB
/
C.c
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
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VALID(word) regexec(&invalid, word, 0, NULL, 0) == REG_NOMATCH
int main() {
char *word, **tmp, **words = NULL;
FILE *txt = fopen("words.txt", "r");
size_t maxlen = 0, wlen = 0;
int num = 0;
regex_t invalid;
regcomp(&invalid, ".*[gkmqvwxzio].*", REG_ICASE|REG_NOSUB);
while (getline(&word, &wlen, txt) != -1) {
wlen = strlen(word);
if(wlen == maxlen && VALID(word)) {
tmp = words;
words = (char **) realloc(tmp, wlen * ++num);
*(words + num - 1) = (char *) malloc(wlen);
strncpy(*(words + num - 1), word, wlen);
} else if(wlen > maxlen && VALID(word)) {
words = (char **) realloc(words, wlen);
*words = (char *) malloc(wlen);
strncpy(*words, word, wlen);
maxlen = wlen;
num = 1;
}
}
fclose(txt);
free(word);
regfree(&invalid);
for (int i = 0; i < num; ++i) printf("%s", *(words + i));
free(words);
}