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 ”)
Answers were Sorted based on User's Feedback
Answer / balaji ganesh
#include<stdio.h>
#include<string.h>
void main()
{
char s[200],c[20],v=' ';
int i=0,j,f,n=0;
printf("enter string: ");
gets(s);
printf("enter new string: ");
gets(c);
while(i<strlen(s))
{
j=f=0;
while(j<strlen(c))
{
if(s[i++]!=c[j++])
{
f=1;i--;break;
}
}
if((f==0)&&(i==strlen(s)||s[i]==' ')&&(v==' '))
n++;
v=s[i++];
}
printf("the word %d times occured",n);
}
Is This Answer Correct ? | 91 Yes | 41 No |
Answer / anil
only balaji's ws de ri8 answer... thanx 2 him...
Is This Answer Correct ? | 23 Yes | 15 No |
Answer / abhishek
#include<stdio.h>
#include<string.h>
main()
{
int i,occ=0;
char str[100],ch;
printf("enter string");
scanf("%s",str);
printf("enter character");
scanf("%s",ch);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
occ+=1;
}
else
{
occ+=0;
}
}
printf("occurance of %s in %s is %d",ch str occ);
getch();
}
Is This Answer Correct ? | 14 Yes | 12 No |
Answer / ranjith kumar
#include<stdio.h>
//#include<conio.h>
void main()
{
int i=0,chr=0,sp=0,words=1,ss=0,digits=0;
char line[100],temp='a';
//clrscr();
printf("\nEnter the line:\n");
gets(line);
while(line[i]!='\0') //to check for string termination
{
if((line[i]>64&&line[i]<91)||(line[i]>96&&line[i]<123)) // ascii range of characters
chr++;
else
{
if(line[i]==32) //ascii value of space is 32
{
sp++;
if(temp!=32)
words++;
}
else
{
if(line[i]>47&&line[i]<58) //ascii range of digits
digits++;
else
ss++;
}
}
temp=line[i];
i++;
}
printf("\nNumber of characters = %d words = %d spaces %d special symbols = %d digits = %d",chr,words,sp,ss,digits);
}
Is This Answer Correct ? | 7 Yes | 8 No |
Answer / 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 |
Answer / ram
#include <string.h>
void main()
{
char name[] ="Dream as if you will live forever, live as if
you will die today";
char r[] ="live";
int iLen = strlen(r);
int iCount = 0;
while(1)
{
if( strstr(name,r) == NULL)
break;
strcpy(name, strstr(name,r));
strcpy(name,name+iLen);
iCount++;
}
printf("%d",iCount);
}
Is This Answer Correct ? | 23 Yes | 28 No |
Answer / vikas kumar from agra
i've try my level best ..plz run this program...but u don't
mistake in typing..100% output right...just try..
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,s,oc=0;
char str[100],substr[30];
clrscr();
printf("\n enter string");
gets(str);
printf("\n enter substr");
gets(substr);
for(s=0;substr[s];s++)
for(i=0;str[i];i++)
{
if(str[i]==substr[0])
{
j=i;
k=0;
while(str[j]==substr[k] && srt[j]!='\0' && substr[k]!='\0')
{
j++;
k++;
}
if(k==s)
{
oc++;
}
printf("\n\t\t occurrence is %d",oc);
getch();
}
Is This Answer Correct ? | 18 Yes | 43 No |
Answer / vikas kumar
#include<stdio.h>
#include<conio.h>
void main()
{
int i,occ=0;
char str[100],ch;
printf("\n enter string");
scanf("%s",str);
printf("\n enter character");
scanf("%s",ch);
for(i=0;i[str]!='\0';i++)
{
if(str[i]==ch)
{
occ++;
}
}
printf("\n occurance of %c in %s is %d",ch str occ);
getch();
}
Is This Answer Correct ? | 25 Yes | 66 No |
write a program to find out prime number using sieve case?
A function can make the value of a variable available to another by a) declaring the variable as global variable b) Passing the variable as a parameter to the second function c) Either of the two methods in (A) and (B) d) binary stream
provide an example of the Group by clause, when would you use this clause
to find out the reverse digit of a given number
6 Answers Infosys, Microsoft, TCS, Wipro,
Write a c program to print the even numbers followed by odd numbers in an array without using additional array
Explain why c is faster than c++?
Why c is procedure oriented?
What is the difference between union and anonymous union?
Write a program to find factorial of a number using recursive function.
Example of friendly function in c++
Explain continue keyword in c
What will the code below print when it is executed? int x = 3, y = 4; if (x = 4) y = 5; else y = 2; printf ("x=%d, y=%d ",x,y);