Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to write a code to find the number of occurence of a word in a file. For some reasons, my code is not working properly. As a beginner, I would like to get some help. Many thanks!!

What I have tried:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(){
	FILE *fptr,*ptr;
	int l,i=0,count=0,total=0;
	char name[50],n,word[25],k;
	printf("\nEnter the file name:");
	scanf("%s",name);
	printf("\nEnter the word to be found:");
	fgets(word,25,stdin);
	l=strlen(word);
	fptr=fopen(name,"r");
	if(fptr==NULL){
		printf("\nProblem with opening the file");
		exit(1);
	}
	n=fgetc(fptr);
	while(!(feof(fptr))){
		if(n==toupper(word[i])||n==tolower(word[i])){
			count++;
			if(count==1){
				ptr=fptr;
			}
			i++;
		}
		else if(n!=word[i]){
			if(count>1){
				fptr=ptr;
			}
			count=0;
			i=0;
		}
		if(count==l){
			total++;
			count=0;
			i=0;
		}
		n=fgetc(fptr);
	}
}
Posted
Updated 7-Jul-20 6:10am
Comments
Patrice T 7-Jul-20 9:52am    
Define "my code is not working properly"
Garth J Lancaster 7-Jul-20 9:55am    
Can you elaborate on "my code is not working properly." .. what is it doing .. also, your code has no comments - commenting code is useful for two reasons - it lets others know what your intent is, and, by the act of commenting you're reading what you've written and seeing if it makes sense

Also - we know nothing about the format of your input file - is it one word per line, or sentences, or ?? there are quicker ways to read the file if its one word per line, and you can avoid character by character reading

Depends what you mean by "better" - it's a very subjective thing.

For me, the first improvement would be to stop using single character variables, and document your code so it's a lot more obvious what you are doing and why.

And I'd probably process the file into separate words, sort them, count them, and store the two items together in a struct with an array of the struct items.

Then the search becomes trivial. And it doesn't count "Partial" words: search yours for "in" and it will count "in" as well as "inside", "acetophenetidin", "bin", "window" and so on as "matches" ...
 
Share this answer
 
Best is to read the whole file and than use strtok. Read the example careful to use it correctly. The function is case sensitive so you may search for your word starting with the upper case too. Create a copy of the word with the first letter as uppercase.
 
Share this answer
 
You can't even begin to handle this before you have well defined the task. What is an occurrence of your word? does it need to have matching case? does it count as part of another word? what about plurals? and hyphenated words (split over two or more lines)?

When these requirements are cleared, put them in comment at the start of your code!

For the implementation, don't handle it one character at a time; I also wouldn't read all of the file at once, there is no need for that; probably dealing with one line of text at a time would be the right approach.

Finally, depending on the requirements, there may be quite some opportunities for optimizaton...

:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900