made work: count the number of words in a file_t*
This commit is contained in:
+21
@@ -256,3 +256,24 @@ fileNumberLines(file_t* f) {
|
||||
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
int
|
||||
fileCountWords(file_t* f) {
|
||||
int num_words = 0;
|
||||
char prev;
|
||||
for (int i = 0; i < f->length; i++) {
|
||||
for (int j = 0; j < ((vector_t*)vectorGetAt(f->lines, i))->size; j++) {
|
||||
char ch = *((char*)vectorGetAt((vector_t*)vectorGetAt(f->lines, i), j));
|
||||
if (
|
||||
(ch == ' ' && prev != ' ') ||
|
||||
(i == f->length-1 && j == ((vector_t*)vectorGetAt(f->lines, i))->size-1) ||
|
||||
(j == ((vector_t*)vectorGetAt(f->lines, i))->size-1 && j != 0)
|
||||
) {
|
||||
num_words++;
|
||||
}
|
||||
prev = ch;
|
||||
}
|
||||
}
|
||||
|
||||
return num_words;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user