This commit is contained in:
KIX
2025-11-08 03:48:18 -05:00
parent b5f9a34935
commit 397d263a96
2 changed files with 65 additions and 0 deletions

BIN
wcl Executable file

Binary file not shown.

65
wcl.c Normal file
View File

@@ -0,0 +1,65 @@
#include <stdio.h>
#include <ctype.h>
int countLines(const char* fname);
int countBytes (const char* fname);
int countWords (const char* fname);
int main(int argc, char *argv[])
{
int nlines = 0;
int nbytes = 0;
int nwords = 0;
for(int i=1; i < argc; i++)
{
nlines = countLines(argv[i]);
nbytes = countBytes(argv[i]);
nwords = countWords(argv[i]);
printf("%s: lines - %d, bytes - %d, words - %d\n", argv[i], nlines, nbytes, nwords);
}
return 0;
}
int countLines(const char* fname)
{
FILE* file = fopen(fname, "r");
int lines = 0;
char buffer[1024];
while (fgets(buffer, sizeof(buffer), file) != NULL)
{lines++;}
fclose(file);
return lines;
}
int countBytes (const char* fname)
{
FILE* file = fopen(fname, "r");
int bytes = 0;
int c;
while ((c = fgetc(file)) != EOF)
{
bytes++;
}
return bytes;
}
int countWords (const char* fname)
{
FILE* file = fopen(fname, "r");
int words = 0;
int c;
int is_in_word = 0;
while ((c = fgetc(file)) != EOF)
{
if (isspace(c))
{is_in_word = 0;}
else
{
if (is_in_word == 0)
{
words++;
is_in_word = 1;
}
}
}
return words;
}