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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -