Question:21
Write a C program to read a file word by word. Further print how many time “this” word is present in the file.
[note: Using the above program we can search any word in the file.]
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
FILE *fp;
char n[30],fname[20];
int i=0,len;
printf("Enter filename to read ");
scanf("%s",fname);
fp = fopen(fname, "r");
//fp = fopen("test10.txt", "r");
if(fp==NULL)
{
printf("Unable to open the file\n");
getch();
return(0);
//exit(1); //header file: stdlib.h
}
while(1)
{
fscanf(fp, "%s", n); //read word by word
if(feof(fp))
{
printf("end of file");
break;
}
if(strcmpi(n,"this")==0)
{
printf("%s\n",n);
i++;
}
}
fclose(fp);
printf("total words = %d\n",i);
getch();
return(0);
}
Question:22
Write a C program to read a file word by word. Further print how many time “is” word is present in the file.
[Using the above program we can search any word in the file.]
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
FILE *fp;
char n[30],fname[20];
int i=0,len;
printf("Enter filename to read ");
scanf("%s",fname);
fp = fopen(fname, "r");
//fp = fopen("test10.txt", "r");
if(fp==NULL)
{
printf("Unable to open the file\n");
getch();
return(0);
//exit(1); //header file: stdlib.h
}
while(1)
{
fscanf(fp, "%s", n); //read word by word
if(feof(fp))
{
printf("end of file");
break;
}
if(strcmpi(n,"is")==0)
{
printf("%s\n",n);
i++;
}
}
fclose(fp);
printf("total words = %d\n",i);
getch();
return(0);
}




