If loop in C not iterating properly -




writing word counter on c. i'm counting number of spaces in string determine number of words. i'm guessing there's wrong if statement. counts number of words , other times it's throwing random numbers? instance "my red dog" has 3 words "i drink rum" has 1 word , "this code makes no sense" has three. it's printing length of strings fine each.

here's part of code in question:

void wordcounter(char string[]) {   int counter = 1;      int = 0;     int length = strlen(string);     printf("\nthe length of string %d", length);      for( i=0;i<length;i++){          if (string[i] == ' ')         {             counter+=1;             ++i;          }         else          {             counter +=0;             ++i;         }     }      printf("there %d words in sentence , equal to: %d", counter, i); } 

the biggest problem posted code incorrectly increments i should not.

for( i=0;i<length;i++){      if (string[i] == ' ')     {         counter+=1;         ++i; // here      }     else     {         counter +=0;         ++i; // here     } } 

neither of //here lines above needed appear trying do. furthermore, entire else-block pointless, modifies nothing except i, shouldn't doing. therefore, more correct approach simply:

for(i=0; i<length; ++i) {     if (string[i] == ' ')         ++counter; } 

this increments counter whenever index space ' ' character. trivial algorithm, probably suffice trying.


counting spaces, not words

your algorithm doesn't count words, counts number of space characters encountered, plus one. mean inputs, such below (quotes used note content in string, not present), not return accurate word-count:

// shoud zero, returns 1 ""   // should zero, returns 4 (three spaces) "   "  // should one, returns 5 (two spaces, either side) "  word  "  // should two, returns 3 (two spaces between) "word  word"  etc.   

a more robust algorithm required. note not solve everything, makes great leaps in getting closers counting call "words". is, non-whitespace characters separated whitespace characters , potentially buttressing end of string.

this uses pointers rather indexes. in opinion easier read, , declutters code indexing syntax, thereby amplifying going on: consuming whitespace, consuming non-whitespace call "word". comments inline should explain going on:

#include <stdio.h> #include <stdlib.h> #include <ctype.h>  int wordcounter(const char *s) {     int counter = 0;      // while haven't reached terminating nullchar     while (*s)     {         // discard whitespace until nullchar or non-whitespace found         (; *s && isspace((unsigned char)*s); ++s);          // if not nullchar, have start of "word"         if (*s)         {             ++counter;              // discard text until next whitespace or nullchar             (; *s && !isspace((unsigned char)*s); ++s);         }     }      return counter; }  int main() {     const char *s1 = "there should 8 words in string";     printf("\"%s\"\nwords: %d\n", s1, wordcounter(s1));       const char *s2 = "  there   should\t\t\tbe  eight\n in\n this\n string too\n ";     printf("\"%s\"\nwords: %d\n", s2, wordcounter(s2));      const char *s3 = "one";     printf("\"%s\"\nwords: %d\n", s3, wordcounter(s3));      const char *s4 = "";     printf("\"%s\"\nwords: %d\n", s4, wordcounter(s4));      return 0; } 

output

"there should 8 words in string" words: 8 "  there   should            8  in   string  " words: 8 "one" words: 1 "" words: 0 

not perfect; better

the prior algorithm still isn't perfect. accurately extract single words require knowledge of punctuation, potentially hyphenation, etc. much, closer appears goal. out of it.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -