1. Write a C program to count the number of occurrence
of
a specific word in the given strings.
(for e.g. Find how many times the word “live” comes in the
sentence “Dream as if you’ll live forever, live as if
you’ll die today ”)
Answer Posted / aashik shrestha
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
char *p;
char s1[10];
char *q;
int i,m,j = 1,l,count = 0;
printf("Enter the text:");
gets(s);
printf("Enter the word you wanna count:");
gets(s1);
p = s;
q = s1;
l = strlen(s);
m = strlen(s1);
i = 0;
while(*(p+i)!='\0')
{
j = 0;m = 1;
while(*(p+i) != ' ' && *(q+j)!= '\0' && *(p+i)!= '\0')
{
if(*(p+i) != *(q+j)){
m= 0;
break;
}
i++;
j++;
}
if(*(p+i) == '\0')
{
break;
}
if(*(p+i) == ' '&& *(q+j) == '\0')
count++;
if(*(p+i)!= ' ')
{
i++;
}
i++;
}
if(m == 1)
count++;
printf("Total occurence of %d\n",count);
return 0;
}
| Is This Answer Correct ? | 4 Yes | 6 No |
Post New Answer View All Answers
What do you know about the use of bit field?
What are the 5 types of inheritance in c ++?
What is a node in c?
A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles
What are directives in c?
How many data structures are there in c?
What is a example of a variable?
Explain what are header files and explain what are its uses in c programming?
code for replace tabs with equivalent number of blanks
What are run-time errors?
If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits.For example if the number that is input is 12391 then the output should be displayed as 23402
What is the auto keyword good for?
Explain what is the benefit of using an enum rather than a #define constant?
How are portions of a program disabled in demo versions?
What does do in c?