Computer Science Canada To count Word and Blank |
Author: | alpesh [ 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(); } |
Author: | jamonathin [ 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. |
Author: | nireksh [ 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--; } if( *(str+strlen(str)-1)==' ') words--; if(*str=='\0') // ...3 words--; while(str[i]) { if (str[i++]==' ') { while(str[i++]==' '); i--; words++; } else letters++; } printf("\nwords=%d",++words); printf("\nletters=%d",letters); getch(); } /* 3. may be books are right... testing is the most difficult phase of development life cycles */ /* TEST CASES ... 1. NULL STRING 2. BLANK SPACES 3. SPACES AFTER END OF THE STRING */ |
Author: | GiffordBruno [ Tue May 25, 2010 8:27 am ] |
Post subject: | Re: To count Word and Blank |
This program is really not working properly....I am trying to do it correct and i will be back soon. Thanks |