This repository has been archived on 2026-05-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
prog-103-p-05-2030/Who_robbed_Thibouvre/fundamentals/count/count.c
T
2026-02-23 01:15:30 +01:00

45 lines
842 B
C

#include <stdio.h>
int count(const char *filename){
FILE *file = fopen(filename, "r");
if(!file) return 1;
int words = 0;
int cara_word = 0; //cara pour compter les words
int cara = 0;
int lines = 0;
int c = fgetc(file);
while(c != EOF){
cara ++;
if(c == '\n') lines ++;
if (c != ' ' && c != '\n' && c != '\t' && c != EOF) cara_word ++;
else {
if (cara_word > 0) words ++;
cara_word = 0;
}
c = fgetc(file);
}
fclose(file);
printf("> characters: %i\n> words: %i\n> lines: %i\n", cara, words, lines);
return 0;
}
/*
int main()
{
return count("example.txt"); // <-- return 0, because example.txt exists
// Expected:
//> characters: 2030
//> words: 103
//> lines: 5
}
*/