forked from KIX/lab1_git
wcl code
This commit is contained in:
65
wcl.c
Normal file
65
wcl.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user