Write a function that accepts a string and returns the number of words in it.

//Accept a String and display word count

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

int count_words(char []);
void main()
{
int i;
char a[80];
clrscr();
printf("Enter any string\n");
gets(a);
i=count_words(a);
printf("The no of words is %d",i);
getch();
}
int count_words(char a[])
{
int wcnt=0,i=0;
while(a[i])
{
for(;a[i]==' ';i++);
if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
wcnt++;
for(;a[i]!=' '&&a[i];i++);
}
return(wcnt);

}

No comments:

Post a Comment

Related Post

Array:
String functions:
General (Loops-for/while)
Write a Program to print reverse of a given number Write a Program to print Product of Digits (eg. 212=2*1*2=4) of a given number Write a C++ Program to Convert Binary into Decimal Number