Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

write a c program to do the following: a) To find the area of a triangle. b) To convert the temperature from Fahrenheit to Celsius. c) To convert the time in hours : minutes : seconds to seconds.

1941


What is the difference between array_name and &array_name?

1237


application areas a 'c' a) operating system b) graphics, interpreter, assembler c) program evalution, communication softwares d) all the above

1009


if (i = 0)printf ("True"); elseprintf("False"); Under what conditions will the above print out the string "True" a) Never b) Always c) When the value of i is 0 d) all of the above

1107


What is wrong with this program statement?

986


What is #define in c?

990


How many types of sorting are there in c?

1009


Why doesn't C support function overloading?

2599


How do you determine the length of a string value that was stored in a variable?

1078


What is struct node in c?

1014


What is array in C

1097


How can I trap or ignore keyboard interrupts like control-c?

1018


What is #line used for?

984


Write a code to determine the total number of stops an elevator would take to serve N number of people.

1187


Differentiate call by value and call by reference?

915