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 is a pointer in c?
What language is windows 1.0 written?
What is modeling?
What is #define size in c?
Explain how can I avoid the abort, retry, fail messages?
Write a C Program That Will Count The Number Of Even And Odd Integers In A Set using while loop
What is a function in c?
What is a good data structure to use for storing lines of text?
What are the 4 types of organizational structures?
What is structure pointer in c?
What does == mean in texting?
Why malloc is faster than calloc?
Add Two Numbers Without Using the Addition Operator
How can you convert integers to binary or hexadecimal?
typedef enum { html, java, javascript, perl, cgi } lang;The above statement defines a : a) Union b) User defined type c) Enumerated variable d) none