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 / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between a string and an array?

703


How can I split up a string into whitespace-separated fields?

564


By using C language input a date into it and if it is right?

567


Which control loop is recommended if you have to execute set of statements for fixed number of times?

805


What is structure padding in c?

621






What are local static variables?

616


Explain pointers in c programming?

629


Tell me what is the purpose of 'register' keyword in c language?

614


Are c and c++ the same?

623


What is atoi and atof in c?

612


How many keywords (reserve words) are in c?

613


How can I use a preprocessorif expression to ?

595


What is dynamic variable in c?

559


Write a program to input the price of 1 burger and the number of burgers eaten by a group of friends .print the total amount to be paid by the group?

574


What is the use of header files?

598