c - Program prints number of chars instead of words -
i'm trying program print out amount of words in test string, it's printing out larger number instead. example, test string has 24 words , program prints out 102 instead. i'm wondering why it's doing that.
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char testval[1024]="this test... test... next sixty seconds test of emergency broadcasting system."; int inword=0; int wordcount=0; int i=0; while(testval[i] != 0) { if (testval[i]==' ') { if (inword) inword=0; } else { if (!inword) inword=1; wordcount++; } i++; } printf("the number of words in testval %d\n",wordcount); return 0; } ./numwords number of words in testval 102
if (!inword) inword=1; wordcount++;
you missed pair of {}
enclose 2 statements. wordcount++;
executed all non-space characters.
Comments
Post a Comment