Tuesday, June 25, 2013

Write a Program to find no of words in given line ?

Problem:
Write a program to no of words in given line , ex give line is "Welcome you All" and result should be printed as 3

Solution:

#include<stdio.h>
#include<conio.h>

void main()
{
char a[100]="this is c language ",i,j,count=0,need_word=1,isalphabet;
clrscr();
printf("\n%s",a);
for(i=0;a[i]!='\0';i++);
for(j=0;j<i;j++)
{
if((a[j]>='a'&&a[j]<='z')||(a[j]>='A'&&a[j]<='B'))
isalphabet=1;
else
isalphabet=0;
if(isalphabet)
{
if(need_word)
{
count++;
need_word=0;
}
}
else
need_word=1;
}

printf("\nthe length of the string is %d",count);
getch();
}


Output:















if any doubt in above coding , please post it

Like the Post? Do share with your Friends.

No comments:

Post a Comment