|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/sternflCode/wis2.c
#include<stdio.h>
#define SZ 100
#define FNAME "/u1/junk/shakespeare.txt"
char word[SZ];
int wordChar(int c) {
if (c=='\n') return 0;
if (c==' ') return 0;
if (c=='\t') return 0;
if (c=='\r') return 0;
return 1;
}
int getNextWord(FILE *f) {
int c;
int i=0;
while ( (c=fgetc(f)) != EOF) {
if (!wordChar(c)) continue;
while (wordChar(c)) {
word[i] = c;
i++;
c = fgetc(f);
}
word[i]=0;
return 1;
}
return 0;
}
int main() {
FILE *f = fopen(FNAME, "r");
int count=0;
while (getNextWord(f))
count++;
printf("Number of words is %d\n", count);
return 0;
}
|