Thread: Count word in a string.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Lightbulb Count word in a string.

    This is a simple program but i got a small problem with it.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    { 
               char str[50];
               int i=0,count=0;
               char space;
               
               printf("Enter a string : ");
             //  gets(str);
               strcpy(str,"d ak");
               printf("%s",str);
                
               while(str[i]!='\0')
               {
               if(str[i]== ' ')
               count++;
               i++;
               }
               printf("\n\nThe total number of words are %d ",count+1);
               
               printf("\n\n");
               system("pause");
               return 0;
    }
    Look at this line: strcpy(str,"d[space]ak");
    Output: 2 words
    But if i change to:
    strcpy(str,"d[space]ak[space][space]d");
    Output: 5 words
    reason: When i increase one space, it will also increase 1 word count.
    example: d[space]d = 2 words
    But if: d[space][space]d = 3words

    How can i solve this?
    Last edited by Alexie; 01-20-2013 at 12:42 AM.

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Your program, as it is, only counts the number of times ' ' occurs in your string. In order to count words, you need to implement a simple finite state machine. Something like this would be sufficient:

    state #1 (in a word):
    Read the terminating null character: break
    Read a non-space character: continue
    Read a space character: change to state #2

    state #2 (out of a word):
    Read the terminating null character: break
    Read a non-space character: change to state #1, increment the word counter, continue
    Read a space character: continue

    Just be sure to initialise the word counter to 0, and the variable which keeps track of the program's state to 2 (or you might use an identifier which takes the form of a predicate, since it's a binary thing.)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... if you just want the count and know what is the maximum length of a word, then a simple solution is to call scanf in a loop, counting the words until there are no words left to read. However, this would only work if you define word as "a sequence of non-whitespace characters", since it would consider "hello,world" as one word rather than two.

    If you want to example the input character by character instead, then you have to be careful too, e.g., "hello world" is 2 words, but a blind counting of spaces might lead you to obtain an output of 3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Thanks for the reply.
    Can you guy help out with my code? I'm still learning in my progress.

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    What book are you using to learn C? There's a good example for what you're trying to do in section 1.5.4 of “The C Programming Language (Second Edition)” by Kernighan and Ritchie.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    One way to approach this is to make a list of input strings and write down somewhere what you are expecting the output to be. For example

    "d ak" // 2
    "d ak" // 2
    "a b c d" // 4

    Then you can just make corrections to the program until it gives you the expected output. Use Barney's idea of "in word" and "out of word" states. This is the approach used in TCPL second edition. A word is not a space, rather a word boundary occurs when one transitions from a space to a non-space or from a non-space to a space.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    I don't have any book to learn. I was learn it through youtube.

  8. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Well, K&R2 is a reasonably popular book. Your nearest library is likely to have a copy, so you might consider borrowing it from somewhere. Otherwise you might be able to get a copy online.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Ok thanks alot. I will try to get a copy soon

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    I manage to solve the first error by change the line to:
    if((str[i]== ' ' && str[i+1] != ' '))

    Input: a[space][space][space]d
    Output: 2 words

    But the next problem I can't solve is:

    Input: a[space][space][space]d[space]
    Output: 3 words

    How come this will happen?

  11. #11
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Well, that happens because '\0' is not equal to ' '. How does it work when the string begins with a space?

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    How are you reading in the input? If you read in a string like "a d " then this will have these characters in a normal null-terminated string:
    Code:
    {'a', ' ', ' ', ' ', 'd', ' ', '\0'}
    How many places are there in this array where your condition str[i] == ' ' && str[i+1] != ' ' is true?

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Oh. so how should i code to let the program know it begin with a space?

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Alexie View Post
    Oh. so how should i code to let the program know it begin with a space?
    You might work it like this. You have a variable that works like a light switch - it's either ON or OFF (1 or 0). Call it "inWord".

    int inWord=0 declares it at the start of the program. Now your program will "walk" through any text and not count words until inWord becomes 0 again. When your program reaches the first letter of the alphabet or a digit. Then inWord becomes 1, since it's inside a word. No word count is changed yet.

    Now as the program moves on through the text, it will STILL not count any words, until it reaches a char that is NOT a letter, or a digit - maybe a space, maybe a comma, maybe a newline, maybe the end of the file (EOF). But when it reaches any of these chars, it will increment the number of words, and at the same time, change inWord, back to zero.

    You might be surprised to learn that C has a standard function to test whether a char is an alphanumeric or not, which is defined in the header file

    #include <ctype.h> //char types header

    Isn't that handy?
    Last edited by Adak; 01-20-2013 at 07:36 AM.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    i did use <ctype.h> for counting space
    Code:
               while(str[len]!='\0') len++;
               for(i=0;i<=len;i++)
    {
            while( isspace( str[i] )) {  space++; i++;  }
            {continue;}
            
    }
              printf("\n\nThe total number of words are %d ",space);
    This is what i include in my code. yea it works well

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