logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/sternflCode/h6/p03.c

/*

p03.c - In this program, you will work with shakespeare.txt

		Using Dr. Sternfeld's getNextWord an wordChar functions (from wis.c in
        the ~sternfl/256F directory) find the longest word in shakespeare.txt
        and also print the length of the word

		Hints: You will also be using strlen from the string.h file. The strlen
        function takes in a string as an argument, and returns it's length.

        You will also use strcpy, to copy what's stored in "word" and put it in longestWord

        shakespeare.txt can be accessed by going to /u1/junk/shakespeare.txt
*/

#include <stdio.h>
#include <string.h>

#define SZ 100 //wherever you see SZ in this code, this means 100

char word[SZ];  // declare an array for reading the word in outside of main,
                // that way, any function in this program will have access to it.
                // the getNextWord function will put words in this array

//prototypes for the functions
//you will fill in the function definitions below main
int wordChar(int c);
int getNextWord(FILE *f);

int main(){

	char longestWord[SZ]; //declare an array to store the longest word

  //open /u1/junk/shakespeare.text for reading
	FILE *f = fopen("/u1/junk/shakespeare.txt", "r");

  int count = 0;            // declare a variable for the count, set this to 0
  int len;                  //declare a variable for the length
  int lenlongest = 0;       //declare variables for length and length of longest word

  //set up a while loop, run that as long as getNextWord returns 1
  while (getNextWord(f)){

    // get the length of "word" using strlen

    //check if length of the current word is longer than the longest word found
        //if so: make, lenlongest the current length
        // then use strcpy to copy whatever is in word into longestword
  }

    //then print the output
  printf("Longest word is length %d\n", lenlongest);
  printf("Longest word was %s\n", longestWord);

  return 0;

}

//use the wordChar and the getNextWord functions from wis.c

// wordChar determines if the given character is an alphabet character
// use the code for this function from wis.c in ~sternfl/256F
int wordChar(int c) {
//put the coode here

}

// getNextWord takes in a file pointer and reads each word in the file
// use the code for this function from wis.c in ~sternfl/256F
int getNextWord(FILE *f) {
//put the code here

}