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
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 |
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 |
What is a 'null pointer assignment' error?
One of the Institutes contains 5 student groups. Every group contains 4 students. Institute wants to store student group’s details in array. Group should contain group member’s details (name and registration number and age), project name, and mark of the group.
actually i have 2 years teaching experience as computer faculty but now i am a DBA but when i go for interview many peoples asked me why i left my teaching profession and why i want to come in this field kindly give me the proper answer of this queston
how the size of an integer is decided? - is it based on processor or compiler or OS?
19 Answers HCL, JPR, Microsoft, nvidia,
How does sizeof know array size?
void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }
What is the purpose of main() function?
write a program in c to read array check element is present or not?
how do you write a function that takes a variable number of arguments? What is the prototype of printf () function?
How do I send escape sequences to control a terminal or other device?
Why is it usually a bad idea to use gets()? Suggest a workaround.
Can we write a program without main() function?