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 |
Is fortran faster than c?
Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above
Write a program using bitwise operators to invert even bits of a given number.
a way in which a pointer stores the address of a pointer which stores the value of the target value a) reference b) allocation c) multiple indirection d) none
What is the difference between malloc() and calloc() function in c language?
Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL
2. Counting in Lojban, an artificial language developed over the last fourty years, is easier than in most languages The numbers from zero to nine are: 0 no 1 pa 2 re 3 ci 4 vo 5 mk 6 xa 7 ze 8 bi 9 so Larger numbers are created by gluing the digit togather. For Examle 123 is pareci Write a program that reads in a lojban string(representing a no less than or equal to 1,000,000) and output it in numbers.
write a program to sum of its digit with using control structure or with out using loop. for ex: let the number is 25634 then answer will be=2+5+6+3+4=20
What is a constant?
What is calloc() function?
what's the return value of malloc()
What is selection sort in c?