counting words in string

This is a discussion on counting words in string within the C Programming forums, part of the General Programming Boards category; please forgive me - this is only my second day writing C. I am writing a basic app that counts ...

  1. #1
    Unregistered
    Guest

    counting words in string

    please forgive me - this is only my second day writing C.

    I am writing a basic app that counts how many words are in a sentance.

    Here's the code.

    /* Program to Determine how many words are in a string */

    #include <stdio.h>
    #incluse <string.h>

    char input_string[200];
    int word_count;


    /* This function will take a string and calculate how many words are in it. ***
    ** It will do this by finding out how many spaces are in the string ***
    */

    int words(char sentence[200])

    {
    int string_length, counter, count_spaces, word_amount;
    string_length = strlen(sentence);
    count_spaces = 0;

    /*Now we loop until end of string length and calculate spaces*/

    for (counter; counter == string_length; ++counter) { /*#### Compiler error here ***/
    if (sentence[counter] == ' ''){
    count_spaces = count_spaces + 1);
    }
    word_amount = count_spaces - 1;

    return(word_amount);
    }

    int main ()

    {

    /* Gets sentence from user */
    printf("Enter in a Sentence: ");
    fgets(input_string, sizeof(input_string), stdin);


    /* sends info to function 'words' to be processed - info will be sent back in integer */

    word_count = words(input_string);

    printf("There are %d words in this sentence\n", word_count);

    return(0);
    }

    Here's the exact error that gcc gives me:

    words.c:24: unterminated character constant

    Any help would be great... thx.

    (FYI - Im compiling under linux)

  2. #2
    Unregistered
    Guest
    oh sorry - the way I am going about trying to find the words is by counting the amount of spaces in the string.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >for (counter; counter == string_length; ++counter)
    I expect you meant to write something like
    >for (counter=0; counter < string_length; ++counter)

    And this is spelt wrong
    >#incluse <string.h>

    And this:
    >if (sentence[counter] == ' ''){
    has three qoutes, when it should have two
    >if (sentence[counter] == ' '){

    This
    >count_spaces = count_spaces + 1);
    should be
    >count_spaces = count_spaces + 1;

    And there's a } missing in the words() function. I'll let you find that one

    Unfortunately, when these problems are corrected, the program gives you the wrong answer, but that'll be for you to debug.... Have fun!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is farily easy. Break it down into logical steps. [edit] You can do this one integer. [/edit]

    Code:
    long int count_words( char *s )
    {
        long int count = 0;
        if( s != NULL )
        {
            while( s && isspace( *s ) ) s++;
            while( *s )
            {
                while( s && !isspace( *s ) ) s++;
                count++;
                while( s && isspace( *s ) ) s++;
            }  
        }
        return count;
    }
    I usually don't do homework for people, but I'm feeling ... something. Anyway, you probably don't want to use this for the simple fact that you'll have to prove to your teacher that you know just what it is exactly that this does.

    To expand that into pseudo code you can understand:

    Code:
    while not at the end of the string (you could use a counter)
        while we have a space, move to the next place in the string
        while we have don't have a space, move to the next place in the string
        increment the word count
    Quzah.
    Last edited by quzah; 05-29-2002 at 07:24 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    I usually don't do homework for people, but I'm feeling ... something.
    You're not going soft are you quzah That's not like you!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer

    You're not going soft are you quzah That's not like you!
    Maybe I'm just tired. Actually it gives me something to do. I throw something together real fast in my head and see if it compiles the first shot. If so, then I try and break it. I've broke my above code trying to eliminate a loop. If I add in another integer, I can make it unbreakable, but the point of my exercise was to do it with a single one.

    The above code worked in my test, but I can break it. The theory is sound though.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest
    lol - its not homework actually - I am self teaching. The book asks me to do this question but it was pretty vague on a few things. Thanks anyways guys - I can play a bit more.

  8. #8
    Unregistered
    Guest

    Thumbs up

    well it werked for me - Don't worry Im sure you'll hear more from me :

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Code:
    int foo(char *str)
    {
     char *s = str;
     int ready = TRUE;
     int account = 0;
    
     while( s && *s )
      {
       if(!isspace(*s))
        {
         if(ready)
          {
           ++account;
           ready = FALSE;
          }
    
        }else
           ready = TRUE;
    
        s++;
      }
    
    return account;
    }
    long int count_words( char *s )
    ...expecting a large count, eh?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > ...expecting a large count, eh?

    Nah. Just getting into the habbit of specifying my integer types. You either have long or short, and 'long' is one less letter to type.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21