#include #include #include #define MAXLINESIZE 80 #define NUMLETTERS 58 #define FIRSTASCIICHAR 65 int totalwords = 0; struct word { char **strword; int *index; }; struct book dictionary; struct book { struct word alpha[NUMLETTERS]; int size[NUMLETTERS]; }; int getline(char *line) { char currchar; int i = 0; currchar = getchar(); if (currchar == EOF){ return(0); } else if(currchar == '\n'){ return(2); } while(currchar != EOF && currchar != '\n'){ line[i] = currchar; i++; currchar = getchar(); } return(1); } void printword (char *word){ int len = strlen(word); for(int i = 0; i < len; i++) { printf("%c", word[i]); } } int findword(char *word, int index){ char *temp; for(int i = 0; i < dictionary.size[index]; i++){ temp = dictionary.alpha[index].strword[i]; if(strcmp(word,temp) == 0){ printf("%d",dictionary.alpha[index].index[i]); return(1); } } return(0); } void AddToBook(char *word, int index){ int size = dictionary.size[index] + 1; dictionary.size[index] = size; if(size != 1){ dictionary.alpha[index].strword = realloc(dictionary.alpha[index].strword, size * sizeof(word)); dictionary.alpha[index].index = realloc(dictionary.alpha[index].index, size * sizeof(int)); } else{ dictionary.alpha[index].strword = malloc(size * sizeof(word)); dictionary.alpha[index].index = malloc(size * sizeof(int)); } strcpy(dictionary.alpha[index].strword[size-1],word); dictionary.alpha[index].index[size-1] = totalwords; totalwords++; } int main(){ char currline[MAXLINESIZE]; char *word; int i = 0, wordint = getline(currline); while(wordint != 0){ i = 0; wordint = -1; word = strtok(currline, " "); while(word != NULL && i < MAXLINESIZE){ if(currline[i] != ' ' && currline[i] != 0){ wordint = findword(word, currline[i] - FIRSTASCIICHAR); if(wordint == 0){ printword(word); AddToBook(word,currline[i] - FIRSTASCIICHAR); } i += strlen(word); word = strtok(NULL, " "); } else{ printf(" "); i++; } } wordint = getline(currline); while(wordint == 2){ printf("\n"); wordint = getline(currline); } if (wordint != 0) printf("\n"); } return(0); }