Write a c program to find, no of occurance of a given word
in a file. The word is case sensitive.

Answer Posted / vadivelt

First provide an input string, which will be saved in a file.
Then provide a word to be searched. So that it will return
the occurance of a given word in the file.

Here am creating a file and writting the data, and performing
search operation. But if you want to find a word in already
existing file please comment the functions fopen(), gets() and
fclose() which are used in write mode ie., "w+". And give the
file path in fopen() which is used in read mode.

Code here.,

#include<stdio.h>
#include<conio.h>

main()
{
FILE *fp;
/*Here ch = 'a' assignment is done only just to enter the
while loop for the 1st time*/
char ch = 'a', *p1, *dupP1, *dupP2, *src, *dupSrc, *pmain;
int count = 0;

pmain = (char *)malloc(1000);
fp = fopen("vel.txt", "w+");

if(pmain != '\0' && fp != '\0')
{
printf("ENTER THE STRING TO BE WRITTEN IN FILE\n");
pmain = gets(pmain);
fputs(pmain, fp);
}

src = (char *)malloc(20);
if(src != '\0')
{
printf("\nENTER THE STRING TO BE SEARCHED IN THE FILE\n");
gets(src);
}

p1 = (char *)malloc(100);
dupP2 = dupP1 = p1;
dupSrc = src;

fclose(fp);

fp = fopen("vel.txt", "r");
while(ch != EOF)
{
ch = fgetc(fp);
if(ch != ' ' && ch != '\n' && ch != EOF)
{
*(dupP1++) = ch;
}
else
{
*dupP1 = '\0';
while(*dupSrc != '\0' && *dupP2 != '\0')
{
if((*dupSrc == *dupP2) )
{
if((*(dupSrc + 1) == '\0'
&& *(dupP2 + 1) == '\0'))
{
count++;
}
}
else
{
break;
}
dupP2++;
dupSrc++;
}
dupP2 = dupP1 = p1;
dupSrc = src;
}
}
printf("\nIN '%d' PLACE(S) THE STRING '%s' AVAILABLE IN \
THE FILE\n", count, src);

fclose(fp);

getch();
}

Is This Answer Correct ?    4 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How are portions of a program disabled in demo versions?

752


What is the role of this pointer?

549


Write a program to show the change in position of a cursor using c

584


What is indirection in c?

629


the number of measuring units from a arbitrary starting point in a record area or control block to some other point a) branching b) recording pointer c) none d) offset

640






What is array of structure in c programming?

756


How to delete a node from linked list w/o using collectons?

2090


What is function and its example?

628


What is a method in c?

629


What is void c?

569


What is the role of && operator in a program code?

568


Explain how do you use a pointer to a function?

641


What should malloc() do?

645


If i have an array 0 to 99 i.e,(Size 100) I place the values 1 to 100 randomly like a[0]=29,a[1]=56 upto array[99].. the values are only between 1 to 100. getting the array values by using scanf.. If i entered one wrong element value line a[56]=108. how can i find it.. and also how to find the missing value in 1 to 100.. and i want to replace the missing values.. any one of them know please post your answer..

1595


What is the most efficient way to store flag values?

690