# Can someone please design the method to find the frequency of occurrences of any given word in a book?

226 views
Can someone please design the method to find the frequency of occurrences of any given word in a book?
posted Oct 11, 2014

You can use < strstr() > to find out the frequency of occurrences of a given word.

Procedure to do:
1:  open your book using    fd =  fopen("book_name", "r");
2:  get your word which you want to find the occurrence.
3:  inside a loop probably    while(till end of file) {}
inside the while()
1.  read line by line (you can use  < fgets() >), then
2.  call strstr()  // validate the required word, if matched increment the count
/*
It will give the location of the first occurrence of the given sub_string (word).
so you have to call this inside a loop (on the result of fgets()) ... to check  multiple occurrence
of the word in a single line.
*/
// see the < man strstr > to know how to use.
3.  follow this till end of the file.
4:  close()  the file descriptor.
You may need to modify a bit to accommodate the case of more then one occurrences of the word in the single line.
Yes you are right, I have already pointed out that modification inside
point no 3's 2nd point.
i.e you have to apply another loop on the resulted line.
Similar Questions

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

How can we perform Sorting a list of strings without using any built-in sort method?

Given a sentence and a word, remove all occurrences of the word in the sentence.

For example, removing “is” from the sentence “This is a boy.” becomes “Th a boy.”

Given a string S and set of strings U. Find the length of longest prefix of S that can be formed using strings in U.

For example you have following -

S= “absolute”
U={“abs”,”xyz”,”ol”,”te”}