'strcmp and strcpy' Not Working Correctly
I'm trying to make program that will read text from a file and show what words occur how many times.
I'm trying to make it so that if a new string comes, it saves it into wlist[], and counts 1 to wcount[]. If the same word is there, it will just add 1 to wcount[]. The program will end when it gets to the end of a file.
I don't know the reason, but I can't get strcmp and strcpy to work.
I had to put a pointer in curword, just to make it compile.
Help and advice is appreciated.
Thank you in advance.
Here is my source code so far:
Code:
#include <stdio.h>
#include <string.h>
#define LIST_MAX 100
#define WORD_MAX 20
int main(){
int i, j, wcount
[LIST_MAX];
char str[15], wlist
[LIST_MAX][WORD_MAX], *curword[20];
FILE *textin;
for(i=0; i<LIST_MAX; i++){
for(j=0; j<WORD_MAX; j++){
wcount[i] =0;
wlist[i][j] =0;
curword[i] =0;
}
}
textin = fopen("assig2-text.txt", "r");
while(fscanf(textin, "%s", str) !=EOF);{
for(i=0;((i<LIST_MAX) && (curword[i]!=0)); i++){
fscanf(textin, "%s", str);
if((strcmp(curword[i], str)) == 0)
break;
}
if(wcount[i]>0){
wcount[i]++;
}
else{
strcpy(wlist[i], str);
wcount[i]++;
}
}
printf("*-- Word Status of 'assig2-text.txt' --*\n");
for(i=0;(i<LIST_MAX) && (wlist[i]!=0); i++){
printf("%s = %d \n", wlist[i], wcount[i]);
}
return 0;
}
Results are like this:
Quote:
*-- Word Status of 'assig2-text.txt' --*
= 1
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0
= 0