made work: count the number of words in a file_t*

This commit is contained in:
Odweta
2023-12-26 09:50:18 +01:00
parent cb04f9a87c
commit f1444de526
5 changed files with 110 additions and 17 deletions
+21
View File
@@ -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;
}