Make a text file todolist.txt as follows,
Go to gym
Go to post office and go back home
Do homework
Wash clothes
Now develop a C program that can count and output the number of word “go” (case insensitive) on to the screen.
Sample output:
File todolist.txt contains 3 go’s.
Hint: use fgets() function.
1.ANSWER
GIVENTHAT:
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int main(void) {
FILE *fp;
char data[100],word[100];
int i=0,count=0;
//open file for reading
printf("Enter word to get its occurence: ");
scanf("%s",word);
//convert word to lowercase
for(i=0;i<strlen(word);++i)
word[i]=tolower(word[i]);
fp=fopen("todolist.txt","r");
while(fscanf(fp,"%s",data)!=-1)
{
//convert string to lowercase
for(i=0;i<strlen(data);++i)
data[i]=tolower(data[i]);
if(strcmp(data,word)==0)
++count;
}
printf("File todolist.txt contain %d %s's\n",count,word);
return 0;
}
Output:-
Get Answers For Free
Most questions answered within 1 hours.