Well unfortunately, I have moved on before this last problem, and no longer have that version original, in my computer.
Try this version and see if it works. It is different though.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct alist {
char word[26];
int count;
};
int main(void) {
FILE * fp;
int i,j,len,totalWords, totalDupes, unique, wasDup;
struct alist list[600];
struct alist temp;
if((fp=fopen("Edelweiss.txt","r")) == NULL) {
printf("Error! File did not open - closing program.\n");
return 1;
}
printf("\n Word Analysis:\n\n Word Frequency\n");
printf(" ============================================\n");
i = totalWords = wasDup = totalDupes = 0;
while((fscanf(fp, "%s", list[i].word)) > 0) {
len = strlen(list[i].word);
while(!isalpha(list[i].word[len]))
list[i].word[len--]= '\0';
for(j=0;j<i;j++) {
if(!strcmp(list[j].word, list[i].word)) { //words match
list[j].count++; //add to the tally
--i;
wasDup=1; //it was a duplicate word
++totalDupes;
}
}
if(!wasDup) //it was not a duplicate word
list[i].count++;
wasDup=0;
++i;
++totalWords;
}
fclose(fp);
unique = (totalWords - totalDupes);
printf("\n Sorted Alphabetically:\n\n");
for(i=0;i<unique-1;i++) {
for(j=i+1;j<unique;j++) {
if(strcmp(list[i].word, list[j].word) > 0) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
for(i=0;i<unique;i++)
printf("%4d. %26s %4d\n",i+1,list[i].word, list[i].count);
printf("\nTotal Words: %d Duplicate Words: %d Unique Words: %d\n",totalWords,totalDupes,unique);
printf("\n\n Press enter when ready, to see the next page of output\n");
getchar();
printf("\n Sorted by Word Frequency:\n\n");
for(i=0;i<unique-1;i++) {
for(j=i+1;j<unique;j++) {
if(list[i].count < list[j].count) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
for(i=0;i<unique;i++)
printf("%4d. %26s %4d\n",i+1,list[i].word, list[i].count);
printf("\n");
return 0;
}
Be sure to copy it ONLY after you hit the Plain Text button.