Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main(int argc, char *argv[]) {
- // read file
- char *content = NULL;
- if (argc != 2 || !(content = get_file_contents(argv[1])))
- exit(EXIT_FAILURE);
- // find the number of words
- unsigned long words_count = 0;
- bool inword = false;
- for (char *s = content; *s; ++s) {
- if (isalpha(*s)) { // C locale: upper, lower letter
- if (!inword) { // new word
- inword = !inword;
- ++words_count;
- }
- }
- else if (inword) {
- inword = !inword; // end of word
- }
- }
- // put words into array
- char* *words;
- if (words_count > (SIZE_MAX / sizeof *words)
- || !(words = (char**)malloc(words_count * sizeof *words)))
- exit(EXIT_FAILURE); // let OS cleanup memory
- char *s = content; // rewind
- for (unsigned long i = 0; i < words_count; ++i) {
- for ( ; !isalpha(*s); ++s); // find start of the word
- words[i] = s;
- for ( ; isalpha(*s); ++s); // find end of the word
- *s = '