Posted: Sat Mar 10, 2007 7:20 am Post subject: To count Word and Blank
Quote:
#include<stdio.h>
#include<conio.h>
void main()
{
char *s;
int i,b=0,w=0;
clrscr();
printf("Enter here your string:");
gets(s);
for(i=0;s[i];i++)
{
if(s[i]==' ')
b=b+1;
if(s[i]>0)
w=w+1;
}
w=w-b;
printf("\nWord=%d\n",w);
printf("Blank=%d\n",b);
getch();
}
Sponsor Sponsor
jamonathin
Posted: Mon Mar 12, 2007 11:20 am Post subject: RE:To count Word and Blank
Cool, maybe now you mite want to hit up the ctype.h predefs and apply some of those to your program.
Oh and for future reference use [ syntax="language"][ /syntax] tags.
nireksh
Posted: Fri Oct 12, 2007 1:25 pm Post subject: RE:To count Word and Blank
probably u calculated
blanks and letters not words and also in ur prog there r some technical mistakes
may be u dont need w
>>w is calculating nothing but stringlength
>>u can calculate that as printf("no. of letters =%d",strlen(s)-b) dirctly to get letters which u say as words
>>u dont even require s[i]>0, putting alone s[i] in if() will work
see my program
/* ...Count letters and words of a string where user can give any
no. of spaces at any place and blank string is also allowed */
#include<stdio.h>
void main()
{
char str[100];
int words=0,letters=0,i=0;
clrscr();
printf("\n"); //ff... for formatting
gets(str);
if(*str==' ')
{
while(str[i++]==' '){ } // `{}' and `;' both will work
i--;
}