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
Is null always equal to 0(zero)?
Is array a primitive data type in c?
What the advantages of using Unions?
Can the curly brackets { } be used to enclose a single line of code?
What is a scope resolution operator in c?
Why is c not oop?
What are the string functions? List some string functions available in c.
Write a program to swap two numbers without using a temporary variable?
What do you mean by a sequential access file?
Explain logical errors? Compare with syntax errors.
What are pointers? Why are they used?
Which function in C can be used to append a string to another string?
Explain the advantages of using macro in c language?
Is there any data type in c with variable size?
Tell us bitwise shift operators?