Monday, July 27, 2009

HOw to count no of words in C language without using pointers and functions?

can any body tell me?


How to count no of words?


i have tried...


like....





to count no of words...i have used words=spaces+1





means ...words are 1 more than a space


but if user enter 2 spaces?


then this program is failed





i cannot use functions and pointers


help me out


waiting for your positive response :)

HOw to count no of words in C language without using pointers and functions?
Start at beginning of file with word count = 0


Loop: until EOF


Skip spaces until first non space found


Increment word count


Skip characters until space found


Goto Loop





You need to put this into C
Reply:You can't use pointers? uhhh...


Little finite-state-machine


Then something like this..didn't test, but it must be close





int state,words=0,i,len=strlen(str);





state=(str[0]==' ');


for(i=1;i%26lt;len;i++) {


if(state==0) {


if(str[i]==' ') {


words++;


state=1;


}


} else {


if(str[i]!=' ')


state=0;


}


}


if(state==0) words++;

online survey

No comments:

Post a Comment