Thursday, September 4, 2008

C program to count no. of characters and words

/* assuming that word separators are , . ' '(space) considering that , . ' '(space) are not characters */

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

void main()
{
int flag=0;
char c;
int no_words=0,no_char=0;

printf("Enter sentence...\n");

while((c=getchar())!='\n')
{
no_char++;
if(c==' '||c=='.'||c==',')
{
if(flag==1)
{
}
else
{

no_words++;

flag=1;
}
no_char--;;
}
else
{
flag=0;
}
}
printf("Number of words are %d",no_words+1);
printf("Number of characters are %d",no_char);
getch();
}

2 comments:

Chaioholic said...

Yet to find a female so engrossed wid C Codes;) kp writin....

Unknown said...

wow great work.. almost all the codes do work after all!