We know that a string is an array of char ending in \0. Your most recent program should have created an array of char. Some entries are alphabetic and others are space. If you go through this array and replace the first of each cluster of spaced by a \0 then you will have a bunch of strings all stored in a large array of char.
Below is code to read in a play, strip the punctuation, convert all the words to lower case, and then keep prompting the user for a word. If the word is found in the play, then the surrounding ten words are printed. Otherwise the user is told of the failure. In either case the program keeps nagging the user until zzz is entered.
You will notice large tracts of code are missing (indicated by ...). Your next mission is to fill in the gaps (or write your own program from scratch). You may find it useful to re-use code from previous exercises.
#include < stdio.h > /* compare two strings, return 0 if identical */ int strcomp(char *s1, char*s2) { while(*s1 == *s2++) if (!(*s1++)) return 0; return 1; } main(argc, argv) int argc; char *argv[]; { FILE *inFile; char text[150000]; // store the entire file, filtered and lower-cased int words[40000]; // store starting addresses of actual words in text[] char uword[20]; // to hold user's word /* input the file whose name is argv[1] ... */ /* code to inserrt a \0 at the end of each actual word ... */ printf("Enter a word > "); scanf("%s", uword); while(strcomp(uword, "zzz")) { i = 0; while ((i < numwords) && strcomp(uword, text+words[i++])); if (i < numwords) { j = i - 5; if (j < 0) j = 0; while (j < i+5) { if (j < numwords) printf("%s ", text+words[j++]); else break; } printf("\n"); } else { printf("%s not found in %s\n", uword, argv[1]); } printf("Enter a word (zzz to quit) > "); scanf("%s", uword); } }
% ex4 richard2.txt Enter a word > to duke of york uncles to the king henry surnamed bolingbroke Enter a word (zzz to quit) > bolingbroke the king henry surnamed bolingbroke duke of hereford son to Enter a word (zzz to quit) > shake if then we shall shake off our slavish yoke imp Enter a word (zzz to quit) > spear slander s venom d spear the which no balm can Enter a word (zzz to quit) > nunnery nunnery not found in richard2.txt Enter a word (zzz to quit) > will brow to brow ourselves will hear the accuser and the Enter a word (zzz to quit) > zzz %
Notice that our program is not very smart about daling with apostrophe s and apostrophe d. See if you can find any other unpleasant quirks about your program. For the purposes of this lab, don't try and fix them.
Discuss the quirks and vagaries of your programs with your lab TAs.