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 |
How are portions of a program disabled in demo versions?
In the following code segment what will be the result of the function, value of x , value of y { unsigned int x=-1; int y; y = ~0; if(x == y) printf("same"); else printf("not same"); } a) same, MAXINT, -1 b) not same, MAXINT, -MAXINT c) same , MAXUNIT, -1 d) same, MAXUNIT, MAXUNIT e) not same, MAXINT, MAXUNIT
simple c program for 12345 convert 54321 with out using string
what is the difference b/w compiler and debugger?
How can you return multiple values from a function?
What are conditional operators in C?
a=0; while(a<5) printf("%d\n",a++); how many times does the loop occurs? a.infinite b.5 c.4 d.6
Write a function stroverlap that takes (at least) two strings, and concatenates them, but does not duplicate any overlap. You only need to worry about overlaps between the end of the first string and the beginning of the second string. Examples: batman, manonthemoon = batmanonthemoon batmmamaman, mamamanonthemoon = batmmamamanonthemoon bat, man = batman batman, batman = batman batman, menonthemoon = batmanmenonthemoon
Can you think of a way when a program crashed before reaching main? If yes how?
what is the difference between entry control and exit control statement?
12 Answers Darbari Lal DAV Model School,
Write a C program where input is: "My name is xyz". output is: "xyz is name My".
out put of printf(“%d”,printf(ram));