edit: I found strtok doenst get the string corrects for some reason how can i fix that?
I have this exercise which states that I must find the occurrence of each word and display it with the word next to it using calloc
(for example for the word "World" that is found 2 times in the text will display like this: World 2).My algorithm doesn't work the output is nonsense for example: "1,78c1,78"
This is my algorithm where I don't know what wrong with it
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
FILE *file=fopen(argv[1],"r");
if(!file) {
perror("File opening failed");
return 1;
}
int size = 1024;
int *wordcount = calloc(size, sizeof(int));
char **words = calloc(size, sizeof(char*));
char line[1024];
int p=0;
int i=0;
while (fgets(line, sizeof(line), file) != NULL) {
char *word = strtok(line, " ");
while (word != NULL) {
for (int j=0;j<i;j++){
if (strcmp(words[j],word)==0){
p=1;
wordcount[j]++;
break;
}
}
if (p==0){
words[i]=strdup(word);
wordcount[i]++;
i++;
}
p=0;
word = strtok(NULL, " ");
}
}
for (int i = 0; i < size; i++) {
if (words[i] != NULL) {
printf("%s %d", words[i], wordcount[i]);
printf("\n");
}
}
fclose(file);
for (int i = 0; i < size; i++) {
if (words[i] != NULL) {
free(words[i]);
}
}
free(words);
free(wordcount);
return 0;
}
Here is one of the inputs I have been given that I have to run through tests.
Computer science is the study of computation, automation, and information.[1] Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (including the design and implementation of hardware and software).[2][3][4] Computer science is generally considered an area of academic research and distinct from computer programming.[5]