Thread: what would I use to count number of words

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    Frankly, I fear you've lead this thread astray.

    You don't need a "Finite State Machine" or convoluted logic to count words. Certainly, no goto's are required (although I use them w/o guilt, as K&R advocated in the White Book).

    To count words, I just:

    Code:
    add_word = 1
    
    while char's remain unread in the string
      read a char in the string.
      
      If it's a letter, and add_word flag is 1 
        increment the word count, 
        set add_word flag to 0
    
      if char is a space, 
        set add_word flag to 1
    
    loop
    This is without actual coding & testing, but I believe it will do the job.

    The add_word variable is key to a simple loop logic for this.

    Adak

  2. #32
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Adak View Post
    Code:
    while char's remain unread in the string
      read a char in the string.
      
      If it's a letter, and add_word flag is 1 
        increment the word count, 
        set add_word flag to 0
    
      if char is a space, 
        set add_word flag to 1
    Pretty much how I'd do it. You might add that the initial state of add_word depends on the first character in the string. I gave a state machine example because it came up as a potential method. And the recursive version was pretty much just showing off

  3. #33
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    Code:
    typedef enum {
        inWord,
        inSpace
    } state_t;
    I missed this originally. According to the standard, all type names ending in _t are reserved. I'm just picking on you

  4. #34
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by brewbuck View Post
    As I said, unless the variable was passed from another function, t
    he compiler can determine whether this is the case.



    So long as that other function is in the same source module, the compiler has no trouble figuring it out. Some compilers, like Intel's ICC compiler, can even perform such analysis across different translation units (.c files).



    For sufficiently complex code, it is far more important for the code to be comprehensible than it is to be 100% "optimal." I've never encountered a case where goto caused previously unacceptably slow code to become acceptable. On the other hand, I have, many times, made simple algorithmic changes which sped up code by factors of 100 or more. And I've got several million lines of code under my belt.



    Maintain it all you want. Whether you're correct or not, most people disagree. Being right isn't going to help you when you get into an argument. People get religious about this stuff.
    Perhaps for the type of code that you need to write it's fine. But for what I do, efficiency is paramount. For real time programming, state machines are usually implemented via look up tables or using the dreaded goto. I'll bet there is a reason that Ragel's state machine compile produces C code that employs gotos!
    http://www.cs.queensu.ca/~thurston/ragel/

    Gotos need not be harder to read than using switch, provided that they are used rationally. If you had a document that contained the state diagram for a FSM and then had well documented C code that implemented the state machine using gotos it should be no harder to understand than using the switch statement, which actually is an extra, unneeded layer of "cruft" for this application IMHO.

  5. #35
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by SevenThunders View Post
    I'll bet there is a reason that Ragel's state machine compile produces C code that employs gotos!
    The reason is that Ragel produces computer-generated code. It's just easier to do it that way. (And yes, I've had this discussion with Ragel's author).

    Gotos need not be harder to read than using switch, provided that they are used rationally. If you had a document that contained the state diagram for a FSM and then had well documented C code that implemented the state machine using gotos it should be no harder to understand than using the switch statement, which actually is an extra, unneeded layer of "cruft" for this application IMHO.
    I'm going to end my participation in this discussion at this point. Write code as you see fit. This is tedious.

  6. #36
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by Adak View Post
    Frankly, I fear you've lead this thread astray.

    You don't need a "Finite State Machine" or convoluted logic to count words. Certainly, no goto's are required (although I use them w/o guilt, as K&R advocated in the White Book).

    To count words, I just:

    Code:
    add_word = 1
    
    while char's remain unread in the string
      read a char in the string.
      
      If it's a letter, and add_word flag is 1 
        increment the word count, 
        set add_word flag to 0
    
      if char is a space, 
        set add_word flag to 1
    
    loop
    This is without actual coding & testing, but I believe it will do the job.

    The add_word variable is key to a simple loop logic for this.

    Adak
    Uh huh. Your state variable is add_word_flag. It's entirely equivalent to brewbuck's Mealy state machine. I happen to think that these kinds of problems, which is a kind of recognizer, are most easily thought of in terms of state machines. What state are we in when we want to increment? What state are we in when we are waiting for a new word, etc. etc. But maybe that's just the way my brain works.

  7. #37
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by brewbuck View Post
    I missed this originally. According to the standard, all type names ending in _t are reserved. I'm just picking on you
    Isn't that POSIX that does that?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #38
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Hmm I don't see any fclose()'s ?

  9. #39
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > According to the standard, all type names ending in _t are reserved.
    Quote Originally Posted by c99 draft
    7.1.3 Reserved identifiers
    1 Each header declares or defines all identifiers listed in its associated subclause, and
    optionally declares or defines identifiers listed in its associated future library directions
    subclause and identifiers which are always reserved either for any use or for use as file
    scope identifiers.
    — All identifiers that begin with an underscore and either an uppercase letter or another
    underscore are always reserved for any use.
    — All identifiers that begin with an underscore are always reserved for use as identifiers
    with file scope in both the ordinary and tag name spaces.
    — Each macro name in any of the following subclauses (including the future library
    directions) is reserved for use as specified if any of its associated headers is included;
    unless explicitly stated otherwise (see 7.1.4).
    — All identifiers with external linkage in any of the following subclauses (including the
    future library directions) are always reserved for use as identifiers with external
    linkage.143)
    — Each identifier with file scope listed in any of the following subclauses (including the
    future library directions) is reserved for use as macro and as an identifier with file
    scope in the same name space if any of its associated headers is included.
    2 No other identifiers are reserved. If the program declares or defines an identifier in a
    context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved
    identifier as a macro name, the behavior is undefined.
    Which of these refers to names ending in _t ?

    > Ragel's state machine compile produces C code that employs gotos
    It's a code generator, the argument doesn't apply. You don't physically write the goto ridden C code, nor do you try to maintain it.
    Same for other code generators like yacc. The output of yacc is a wonder to behold.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #40
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    Which of these refers to names ending in _t ?
    Perhaps I am entirely hallucinating this requirement. As Mr. Sinkula said, maybe it's from POSIX, not standard C. If so, I've got a few butts to kick, mainly my own.

  11. #41
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    hey buddy u just need to count da words, so here is a bit of code, m naive here but just tried my best to put out dis 4 u man
    Code:
    char a[100];
    int count=0;
    printf("Enter the sentence:");
    gets(a);
    for(i=0;a[i]!='\0';i++)
    {
    if(a[i]==32)  //checks if it is space
    {
    if(i!=strlen(a))     //  it must not be the space after the last word
    {
    if(a[i]==a[i-1])   //checks if two of more spaces are in sequence
    count++;
    }
    }
    }
    work with dis n give me feedback buddy

  12. #42
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    Let's see
    - using gets(). This is a big no no - see the FAQ
    - poor indentation. It's only 10 lines, and it's already hard to read.
    - assuming space is 32 - try == ' ' rather than == 32
    - script kiddie English. Enough said.
    - if(i!=strlen(a)) - How many times are you calling strlen in this loop? What about looking for the \0 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #43
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    @ yuuh , read this
    Word counting
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 10:39 PM
  5. counting the number of words
    By ccoder01 in forum C Programming
    Replies: 3
    Last Post: 05-29-2004, 02:38 AM