Tuesday, October 16, 2012

Count number of words in a string


Program to count number of words in a string enter by a user 


#include<stdio.h>

#include<conio.h>

#include<string.h>
void askstring()
{
char str[50];
int i,words=0;
printf("\nEnter a string\n");
scanf("%[^\n]c",str);
//printf("\nThe String is: %s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
words++;

}
printf("\n Number of words in the stirng :%d",words+1);
}
void main()
{
clrscr();
askstring();
getch();
}

No comments: