Thread: Count word in a string.

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So walk through the text - but use the variable inWord, and your count will be correct in ALL cases. You increment a word ONLY when inWord is 1, and you reach a non alphanumeric char.

    You can't just count spaces - that will fail, and you can't make it NOT fail, without a variable like inWord.

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Alternatively you can add your own list of characters that all can separate a word (by your definition). For example:

    Code:
    const char *punctuation = ".,!? \t\n";  // not the best name but..
    
    // then you can test if your character is in the set with
    if( strchr(punctuation, string[i]) )

  3. #18
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Hey Adak! Thanks for your explanation. I got my code working.
    Code:
              if((str[i]== ' ' && str[i+1] != ' ' && inWord++ )) 
      and also
             printf("\n\nThe total number of words are %d ", count); // removing the +1
    Now my input: strcpy(str," aaa 111 === ");
    Output: 3 words 4 spaces

    wee~

  4. #19
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by Subsonics View Post
    Alternatively you can add your own list of characters that all can separate a word (by your definition). For example:

    Code:
    const char *punctuation = ".,!? \t\n";  // not the best name but..
    
    // then you can test if your character is in the set with
    if( strchr(punctuation, string[i]) )
    Thanks for that too. good idea

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Alexie View Post
    Hey Adak! Thanks for your explanation. I got my code working.
    Code:
              if((str[i]== ' ' && str[i+1] != ' ' && inWord++ )) 
      and also
             printf("\n\nThe total number of words are %d ", count); // removing the +1
    Now my input: strcpy(str," aaa 111 === ");
    Output: 3 words 4 spaces

    wee~
    But you're not all set - you need to use isalpha() with each char.

    And inWord doesn't get ++, it gets set either to 0 or 1. Just incrementing inWord will keep inWord testing as TRUE (non-zero), all the time. You don't want that.

    I wouldn't recommend using strstr() (oops! strchr()). It's a fine function, but not for this - imo.
    Last edited by Adak; 01-20-2013 at 08:34 AM.

  6. #21
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Adak View Post
    I wouldn't recommend using strstr(). It's a fine function, but not for this - imo.
    strchr(), but it's convenient if you would want to define "word separators" or what is to be considered a word your self.

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    True, but the OP is already using ctype.h so that's already done. Just needs the inWord flag to handle multiple spaces and other devious string concoctions.

  8. #23
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well I agree that isalpha() is probably a good assumption in most cases, but a word like "real-time" or "O'leary" and the like would be counted as 2 words for example. Then the text may be a C source file or something similar with other non alphabetical chars that you may want to have included. Anyway, I just left it there as an alternative.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are problem chars (like hyphen and apostrophe) for sure, but adjusting the input address to work with strchr() would be beyond the OP wouldn't it?

  10. #25
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    After looking at the code again. I think i got a problem with it again.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include <ctype.h>
    
    int main()
    { 
               char str[50];
               int i=0,count=0;
               int space = 0;
               int len = 0;
               int inWord = 0;
               
               printf("Enter a string : ");
              // gets(str);
               strcpy(str,"Testing one two three four");
               printf("%s",str);
                     
               while(str[i]!='\0')
               {
               if((str[i]== ' ' && str[i+1]!= ' ' && inWord++ )) 
               count++;
               i++;        
               }    
               printf("\n\nThe total number of words are %d ", count);
               
               while(str[len]!='\0') len++;
               for(i=0;i<=len;i++)
    {
               while( isspace( str[i] )) {  space++; i++; }
               {continue;}
            
    }
              printf("\n\nThe total number of spaces are %d ",space);
               printf("\n\n");
               system("pause");
               return 0;
    }
    input: [space]Testing one two three[space]
    output: 4 words
    But
    input:Testing one two three
    ouput: 2 words

    I think i'm getting confuse with the coding.

  11. #26
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    i have do a research on inword = 1; else inword = 0; that all the number and char = 1 and space = 0; how can i do it on my code?

  12. #27
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think the problem is you are not considering the value of `inWord' in your procedure. Rather than starting to write code, consider a pseudocode algorithm and make sure it makes sense first:

    Code:
    wordCount := 0
    inWord := false
    c := (Get Next Character from Input)
    If isalpha(c):
         inWord := true
    End If
    If !isalpha(c):
        If inWord:
            wordCount := wordCount + 1
        End If
        inWord := false
    End If
    You have to continually repeat starting from line 3 until you get to the end of the input. Also for the above to work, the last character of the input must be a non-alpha character ('\0' and '\n' are non-alpha characters)

  13. #28
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    OK thanks . i manage to solve the problem already

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  2. word count help
    By regimental in forum C Programming
    Replies: 7
    Last Post: 03-05-2010, 08:47 AM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM