Thread: Returning count of spaces in a text

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    32

    Returning count of spaces in a text

    Hi.
    I am using:

    Code:
    int retcount (char * text)
    {
    char *tmp = text;
    int count = 0
    while (strtok(tmp, " "))
    count++;
    return count;
    }
    What do you think? Is this gonna be okay?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just keep it simple?
    Code:
    for( count = 0; text; text++ )
    {
        if( *test == ' ' )
            count++;
    }
    Of course you could do...
    Code:
    for( count = 0; strchr( text ); text++, count++ );
    Or maybe...
    Code:
    int count( char *s )
    {
        return s ? *s == ' ' ? 1 + count( s + 1 ) : 0 + count( s + 1 ) : 0;
    }
    That'll get you started.

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

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    for( count = 0; text; text++ )   /* probably *text ? */
    
    for( count = 0; strchr( text ); text++, count++ );  /* strchr() wrong usage or pseudo code?*/

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, and no (and no).

    Edit: Yes, no (and no). I didn't use it enough. See below.


    Quzah.
    Last edited by quzah; 05-23-2010 at 07:33 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by quzah View Post
    No, and no (and no).


    Quzah.
    Amazing :P. Three pieces of code, of which none actually works :P.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by EVOEx View Post
    Amazing :P. Three pieces of code, of which none actually works :P.
    I was tired and on the way to bed.
    Code:
    for( count = 0; *text; text++ )
    {
        if( *test == ' ' )
            count++;
    }
    And...
    Code:
    for( count = 0; strchr( text, ' ' ); text += 1+(strchr( text, ' ' )-text), count++ );
    For some reason I was thinking I was updating text. Don't think while tired.
    Code:
    int count( char *s )
    {
        return s && *s ? *s == ' ' ? 1 + count( s + 1 ) : 0 + count( s + 1 ) : 0;
    }
    1. Tired is bad.
    2. You really should probably compile what you write.

    Quzah.
    Last edited by quzah; 05-23-2010 at 07:34 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  2. Reading Text File (spaces problem)
    By kekewong in forum C Programming
    Replies: 1
    Last Post: 04-15-2009, 03:34 PM
  3. text processing assignment
    By nellosmomishot in forum C Programming
    Replies: 28
    Last Post: 11-25-2008, 03:56 PM
  4. Count number of lines in a text file
    By eXeCuTeR in forum C# Programming
    Replies: 3
    Last Post: 04-30-2008, 01:46 AM
  5. Replies: 3
    Last Post: 05-25-2005, 01:50 PM