I'm attempting to make a C program that builds a concordance from a piece of text.
I need my program to:
1. read the text consisting of one more lines from standard input.
2. parse the lines into individual words and store each word in lower case into a word table.
3. print all the words stored in the table in alphabetical order.
4. empty the word table.
The punctuation in the text cannot be included (except single quotations, ie: sheep's, you're, etc). If a word is used twice in the text, only include it in the table once. The word needs to ignore the casing, ie: Boy and boy are the same word.
The wor dtable should be implemented as an array of pointers to strings. The size of the word table, table_size, should be specified as a command line argument. The program needs to read a file and use input redirection to read it. If the word table becomes full, the program needs to issue a message, "Error" then print the words currently stored in the table in the sorted order. The program needs to search for the absence or presence of a word through binary search.
This is my coding thus far:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
/* A number list is a list of numbers. A numbernode is a node of a
number list which consists of a number and a link to the next numbernode. */
typedef struct numbernode {
int number;
struct numbernode *link;
} numbernode;
/* The wordlist is a list of words, and associated to each word is a list of
numbers (the numbers indicating where the word occurs). Thus, a wordnode
specifies the word, the list of numbers, and a link to the next word. A tail
pointer is included since each time a new number is inserted, it is inserted
at the end of the list. The wordlist is kept in alphabetical order. */
typedef struct wordnode {
char *word;
numbernode *numberhead;
numbernode *numbertail;
struct wordnode *link;
} wordnode;
/* concordance is the pointer to the beginning of the word list. Initially,
of course, it is NULL. */
wordnode *concordance = NULL;
/* getword reads a word from the input stream. It returns true if
it finds a word, but false when it reaches the end of the file. The
parameter w points to a string buffer to store the word in. */
int getword(char *w) {
char c;
while ((c=getchar()) != EOF && !isalpha(c)); // skip over nonletters
if (c==EOF) // finally, we're done!
return false;
else {
*w++ = tolower(c); // save the first char
while ((c=getchar()) != EOF && isalpha(c))
*w++ = tolower(c); // save succeeding chars
*w = '\0'; // nil terminate the string
return true;
} // if/else
} // getword
/* makewordnode creates a new word node where the word being
stored therein is referred to by w. */
wordnode *makewordnode (char *w) {
wordnode *temp;
temp = (wordnode *) malloc(sizeof(wordnode));
temp->link = NULL;
temp->numberhead = NULL;
temp->numbertail = NULL;
temp->word = (char*) malloc(strlen(w));
strcpy(temp->word,w);
return temp;
} // makewordnode
/* findword looks through the list of words for the word specified
by wordbuffer. If the word is not found, then a new word node is
created and inserted into the list of words. In any case, a pointer
to the wordnode (either newly created or already existing) is
returned. */
wordnode *findword(char *wordbuffer) {
/* Fill in the body of this function */
} // findword
/* makenumbernode creates a node with a given number it, and
returns a pointer to that node. */
numbernode *makenumbernode(int n) {
/* Fill in the body of this function */
} // makenumbernode
/* insertnumber inserts a new numbernode with a given number into
the list for a given word. */
void insertnumber(wordnode *w, int n) {
if (w->numberhead) {
w->numbertail->link = makenumbernode(n);
w->numbertail = w->numbertail->link;
}
else
w->numberhead = w->numbertail = makenumbernode(n);
} // insertnumber
/* printword prints to the output stream a word along with its
list of numbers */
void printword(wordnode *w) {
numbernode *n;
printf("%s", w->word);
for (n=w->numberhead; n; n=n->link)
printf(" %d", n->number);
printf("\n");
} // printword
/* printconcordance prints the entire concordance to the output
stream, word by word, with the list of numbers printed for each word. */
void printconcordance(void) {
wordnode *w;
for (w=concordance; w; w=w->link)
printword(w);
} // printconcordance
int main(void) {
char wordbuffer[80];
int wordnumber = 0; // count the words as we read them
printf("\nHere is the concordance for your text.\n");
printconcordance();
printf("\n--------------------\n");
} // main
Any help with implementing some of the things listed above, and helping me improve this code would be greatly appreciated.