C program to calculate the number of words in a given string.

This is a program to count  the number of words in a string entered by the user.I am performing this task by using ASCII value of lower case letters (as the string , user will enter must be in lower case only) .
To count the number of words we are not considering any other character other then lowercase letters.  
To enter the text we are using %[^\n]s as %s terminates on entering space ('   ') to resolve this problem we can also use gets but i am using %[^\n]s here.




#include<stdio.h>
#include<string.h>
int main()
{
   char str[100],c=0,i=0; 
   scanf("%[^\n]s",str);
 while(str[i++]!='\0')
{
if(str[i]<97 || str[i]>123)
{  if(str[i+1]>=97 && str[i+1]<=123)
     c++;
}

}
printf("no of worlds=%d",c);
return 0;
}

Comments :

Post a Comment