logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/sternflCode/wis3.c

#include<stdio.h>

#define SZ 100000
#define FNAME "/u1/junk/shakespeare.txt"

char word[SZ];
int i=0;  //beginning of free space!

int wordChar(int c) {
  if (c>='a' && c<='z') return 1;
  if (c>='A' && c<='Z') return 1;
  return 0;
}

int getNextWord(FILE *f) {
  int c;
  while ( (c=fgetc(f)) != EOF) {
    if (!wordChar(c)) continue;
    while (wordChar(c)) {
      word[i] = c;
      i++;
      c = fgetc(f);
    }
    word[i]=0;
    i++;
    return 1;
  }
  return 0;
}



int main() {
  FILE *f = fopen(FNAME, "r");
  int count=0;
  int i0 = i;
  while (getNextWord(f)) {
    count++;
    printf("%s\n", word+i0);
    i0=i;
    if (count>20) break;
  }
  printf("Number of words is %d\n", count);
  return 0;
}