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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / ashutosh shashi

//function return occurance of word
int findoc(char* str,char* str1)
{
char* temp = str1;
int count =0;
while(*str != NULL)
{
while((*str == *temp)&&(*temp != NULL))
{
str++;
temp++;
if(*temp == NULL)
count++;
}
temp = str1;
if(*str != *temp)
str++;
}
return count;
}

///if function is called like

char* str = "ashashushuaashu";
char* str1 = "ashu";
int count = findoc(str,str1);
printf("%d",count);
///it prints 2, because str1 is appears 2 times in str

Is This Answer Correct ?    1 Yes 5 No

Post New Answer

More C Interview Questions

write a C program:There is a mobile keypad with numbers 0-9 and alphabets on it. Take input 0f 7 keys and then form a word from the alphabets present on the keys.

1 Answers   91mobiles, Amazon, App Guruz, College School Exams Tests, Folio3, Infosys, Omega, Planin, Riphah International University, Subex,


can anyone please tell about the nested interrupts?

0 Answers  


Hai friends im a i year student. i want to develop my knowledge in the field of TSR in c. How I'm Improve ?

2 Answers  


Why is the code below functioning. According to me it MUST NOT.

1 Answers  


What is the use of c language in real life?

0 Answers  






What is an identifier?

0 Answers  


How can I find the modification date of a file?

0 Answers   Celstream,


What is f'n in math?

0 Answers  


Write a program to implement queue.

0 Answers   Aricent,


A B C D E F G F E D C B A A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A

2 Answers  


What is wrong with this declaration?

0 Answers  


Sir,please help me out with the code of this question. Write an interactive C program that will encode or decode multiple lines of text. Store the encoded text within a data file, so that it can be retrieved and decoded at any time. The program should include the following features: (a) Enter text from the keyboard, encode the text and store the encoded text in a data file. (b) Retrieve the encoded text and display it in its encoded form. (c) Retrieve the encoded text, decode it and then display the decoded text. (d) End the computation. Test the program using several lines of text of your choice.

0 Answers  


Categories