im trying to find out the number of different words of a text in a file, using dynamic memory allocation. however, i dont get the right results. the text can contain punctuation. the program is below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int different_words(FILE *fp);
int main(int argc, char *argv[]) {
FILE *fp;
different_words(fp);
return 0;
}
int different_words(FILE *fp) {
int i,j,ic=0,sum=0,sum2=0;
char d[100];
char **A;
fp=fopen("file.txt","rt");
if ((fp = fopen("file.txt", "rt"))== NULL) { //opening the file
printf("cannot read file\n");
exit(1);
}
while (fscanf(fp,"%s",&d)!=EOF)
sum++;
A=(char**)malloc(sum*sizeof(char*)); //allocate memory for all the words
if (A==NULL) {
fclose(fp);
return 0;
}
rewind(fp);
while(fscanf(fp,"%s",&d)!=EOF){
if (strchr("!?.,:",d[strlen(d)-1])==0) //edit
A[ic]=(char*)malloc(strlen(d)+1);
else
A[ic]=(char*)malloc(strlen(d));
if (A[ic]==NULL) {
fclose(fp);
return 0;
}
if (strchr("!?.,:",d[strlen(d)-1])!=0)
for (j=0;j<=strlen(d)-2;j++)
A[ic][j]=d[j];
else
strcpy(A[ic],d);
if (++ic==sum)
break;
}
for (i=0;i<sum;i++){
for (j=0;j<i;j++){
if (strcmp(A[i],A[j])==0)
break;
}
if (i==j) {
sum2++; //finding the number of different words in the text
}
}
printf ("Number of different words in the text: %d\n",sum2);
return sum2;
}
----------