Two little questions:
What means "len"? Length? Is it an "int"?
What function has "len = strlen(list[i].word);"? PS: I know the meaning of strlen, but why do I use it here? Just to put an '\0' to the end of the string?
Two little questions:
What means "len"? Length? Is it an "int"?
What function has "len = strlen(list[i].word);"? PS: I know the meaning of strlen, but why do I use it here? Just to put an '\0' to the end of the string?
Last edited by Fresa; 02-29-2012 at 07:37 AM.
It stopped by word 336.
I tried an other textfile. It consists just 50 times the same word.
However, now, there is no error-message (in contrast to Demodatei.txt). It print out 50 times this word and then the total count:50 and then total duplexes: 1225... strange...
Several changes are needed. Study this:
Edelweiss.txt is this: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, wasDup; struct alist list[600]; 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 = 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 } } if(!wasDup) //it was not a duplicate word list[i].count++; wasDup=0; //printf("%4d. %26s %4d\n",i+1,list[i].word, list[i].count); ++i; ++totalWords; } fclose(fp); for(j=0;j<i;j++) printf("%4d. %26s %4d\n",j+1,list[j].word, list[j].count); printf("\n Total Words: %d\n", totalWords); return 0; }
Edelweiss, edelweiss, every morning you greet me.
Small and white, clean and bright, you look happy to meet me.
Blossom of snow may you bloom and grow,
bloom and grow forever.
Edelweiss, edelweiss, bless my homeland forever.
Wow thanks. I don't understand it 100%, but before I will bombard you with lots of questions I want to brood over your code some minutes more.
But looks it on your computer like this, too?
No, but that may have not been the version I meant to post.
Output: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, wasDup; struct alist list[600]; 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); for(j=0;j<i;j++) printf("%4d. %26s %4d\n",j+1,list[j].word, list[j].count); j = (totalWords - totalDupes); printf("\nTotal Words: %d Duplicate Words: %d Unique Words: %d\n",totalWords,totalDupes,j); return 0; }
Code:Word Analysis: Word Frequency ============================================ 1. Edelweiss 2 2. edelweiss 2 3. every 1 4. morning 1 5. you 3 6. greet 1 7. me 2 8. Small 1 9. and 4 10. white 1 11. clean 1 12. bright 1 13. look 1 14. happy 1 15. to 1 16. meet 1 17. Blossom 1 18. of 1 19. snow 1 20. may 1 21. bloom 2 22. grow 2 23. forever 2 24. bless 1 25. my 1 26. homeland 1 Total Words: 37 Duplicate Words: 11 Unique Words: 26
Last edited by Adak; 02-29-2012 at 09:01 AM.
Sorry I compared my code several times with yours and I can't find my mistake. But I have a similar output like I postet some minutes ago.
If I try to open my Demodatei.txt the program "crashed".
No I just copied your code (into microsoft visual studio) I get this. It looks better, but the frequency ist not correct:
This is my code:
Where is the critical and unseen mistake???Code:#include <stdio.h> #include <string.h> #include <ctype.h> struct alist { char word[26]; int count; }; int main(void) { FILE * fp; int i, wasduplex, j, len, total, totalduplex; struct alist list[600]; if((fp=fopen("Edelweiss.txt","r+")) == NULL) { printf("Error! File did not open - closing program.\n"); return 1; } else { printf("\n Word Analysis:\n\n Word Frequency\n"); printf(" ==========================================\n"); i = total = wasduplex = totalduplex = 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)) /* == 0*/) { list[j].count++; i--; wasduplex=1; totalduplex++; } } if(!wasduplex) {list[i].count++;} wasduplex=0; i++; total++; } fclose(fp); for(j=0;j<i;j++) {printf("%4d %26s %4d\n",j+1,list[j].word, list[j].count);} j = (total - totalduplex); printf("\nTotal Words: %d Duplicate Words: %d Unique Words: %d\n",total,totalduplex,j); } fflush(stdin); getchar(); return 0; }
You removed the ! from the strcmp() line of code, is the first thing I see.
Oh no!!! That's not true!
Okay, now it is the same like if I copy your code... oh noooo... I hate such things! This little sign destroys the whole program!
Okay, but the frequency is still not shown correct(ly).
Does your program work with the "Demodatei.txt", which I have uploaded last page?
Do you sleep sometimes? It's crazy, that you can help me the whole day. But I didn't want to deplore it !!
Last edited by Fresa; 02-29-2012 at 09:38 AM.
You put the ! back into the strcmp() line of code, right?
Yes. And now it doesn't look like post #34 anymore, but it looks like post #36. The frequency seems to have the wrong type, but there is no difference to your code...
Of course there is a difference!
You're messing with me, right?
Post the code as it is right now, and I'll see what I can find, in more detail.
Okay, here is the code, but I compared it lots of times... ok, I haven't also seen the missing "!"... but... look:
Code:#include <stdio.h> #include <string.h> #include <ctype.h> struct alist { char word[26]; int count; }; int main(void) { FILE *fp; int i, wasduplex, j, len, total, totalduplex; struct alist list[600]; if((fp=fopen("Edelweiss.txt","r+")) == NULL) { printf("Error! File did not open - closing program.\n"); return 1; } else { printf("\n Word Analysis:\n\n Word Frequency\n"); printf(" ==========================================\n"); i = total = wasduplex = totalduplex = 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'; } // k = i; for(j=0;j<i;j++) { if((!strcmp(list[j].word, list[i].word)) /* == 0*/) { list[j].count++; i--; wasduplex=1; totalduplex++; } } if(!wasduplex) {list[i].count++;} wasduplex=0; i++; total++; } fclose(fp); for(j=0;j<i;j++) {printf("%4d %26s %4d\n",j+1,list[j].word, list[j].count);} // printf("\ntotal count: %d",i); // printf("\ntest: total duplexes: %d",d); j = (total - totalduplex); printf("\nTotal Words: %d Duplicate Words: %d Unique Words: %d\n",total,totalduplex,j); // printf("\n"); } fflush(stdin); getchar(); return 0; }
Can you find the mistake/difference or not?
I have found out two new problems.
- If you write " ... " in your file, the program doesn't work (three points included of two blank spaces).
- If there will be missing a blank space after a point, so program does not identify the word correct. Example: "The dog is black.The cat is white." The computer doesn't list "black" and "The", but "black.The" ...
I hope I don't needle you
Make your line 39, the same as mine -- exactly. I think it's goofed on that one line, even though they look almost the same, your compiler is treating it differently.
That's part of the definition of a word - one or more letters, terminating in a punctuation mark, an end of line (newline to us), or a space.
There is no such word as "black.The", so naturally I didn't include such a provision in my example program. . . . will be handled. I did expect that detail - just putting it off for now.
Get this right, and then on to sorting the names, and sorting by frequency of the words.
Last edited by Adak; 02-29-2012 at 10:29 AM.
no success:
EDIT: I can copy your whole code: same problemCode:#include <stdio.h> #include <string.h> #include <ctype.h> struct alist { char word[26]; int count; }; int main(void) { FILE *fp; int i, wasduplex, j, len, total, totalduplex; struct alist list[600]; if((fp=fopen("Edelweiss.txt","r+")) == NULL) { printf("Error! File did not open - closing program.\n"); return 1; } else { printf("\n Word Analysis:\n\n Word Frequency\n"); printf(" ==========================================\n"); i = total = wasduplex = totalduplex = 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'; } // k = i; for(j=0;j<i;j++) { if(!strcmp(list[j].word, list[i].word)) { list[j].count++; i--; wasduplex=1; totalduplex++; } } if(!wasduplex) {list[i].count++;} wasduplex=0; i++; total++; } fclose(fp); for(j=0;j<i;j++) {printf("%4d %26s %4d\n",j+1,list[j].word, list[j].count);} // printf("\ntotal count: %d",i); // printf("\ntest: total duplexes: %d",d); j = (total - totalduplex); printf("\nTotal Words: %d Duplicate Words: %d Unique Words: %d\n",total,totalduplex,j); // printf("\n"); } fflush(stdin); getchar(); return 0; }
Last edited by Fresa; 02-29-2012 at 10:29 AM.