#include<iostream.h>
#include<stdio.h>
void main()
{
char a[50];
int i,count=0;
cout<<"enter the string:";
gets(a);
for(i=0;a[i]!='\0';++i)
{
if(a[i]!=' ')
{
if(a[i]!='\0')
{
count++;
}
}
}
cout<<"\nthe number of words in the string  ="<<count;
}

we simply check werther a given word is space or null character if no then we increment the value of count if yes then the loop stops or next increment takes palce.