c - To count the number of letters in a word and no of words in a sentence -
i trying experiment counting number of letters in word.to distinguish between words in string checking blank spaces.if encounters blank space 1 word , has respective letters. example "hello world". output supposed like
o/p hello has 5 letters world has 5 letter
but when trying write code getting segmentation fault. below code.
#include <stdio.h> #include <string.h> main(void) { int nc = 0; int word = 0; char str[] = "this test"; int len = strlen(str); int i; for(i = 0; < len; i++) { ++nc; if(isspace(str)){ ++word; } } printf("%d\n",nc); }
try this..
for(i = 0; < len; i++) { if(isspace(str[i])) { ++word; continue; } ++nc; } if(len>0) word++; printf("%d %d\n",nc, word);
Comments
Post a Comment