Thread: number of substring occurences

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    number of substring occurences

    Hello,
    I wrote function that is supposed to count substring occurences in a line of text. here's my solution:
    Code:
    size_t count_substring ( const char* source, const char* substr )
    {
    	size_t count = 0;
    	char* p;
    	
    	for ( p = source; *p; p++ )
    	{
    		int i = 0;
    		char* t = p; 
    		
    		while ( substr[i] != '\0' )
    		{
    			if ( *t != substr[i] )
    			{
    				break;
    			}
    			t++;
    			i++;
    		} 
    		if ( substr[i] == '\0' )
    		{
    			count++;
    			p = --t;
    		}
    	}
    	return count;
    }
    And here is test code
    Code:
    int main ( void )
    {
    	char line[BUFSIZ];
    	printf ( "Enter line of text: ");
    	fgets ( line, sizeof line, stdin );
    	printf ( "\nTotal number is %u", count_substring ( line, "sam" ) );
    	return 0;
    }
    I wonder if this could be done more elegant. I'd like to hear your advices...
    Thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I say take advantage of the standard library functions when possible:
    Code:
    unsigned int substr_count(char *str, char *substr)
    {
      unsigned int count = 0;
      size_t sublen = strlen(substr);
    
      while((str = strstr(str, substr)))
      {
        str += sublen;
        count++;
      }
    
      return count;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Very elegant, great solution, thanks.
    I'm just wondering, why did you change return type from size_t to unsigned int?

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well they are almost the same thing. IIRC size_t is just an unsigned long.
    Woop?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    why did you change return type from size_t to unsigned int
    Sorry, I didn't really pay much attention to the original function while writing mine. I tend to use size_t when using that variable as a size. I would have used size_t if the function's result was going to be used in the calculation for the size of an array or some such.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Yes, that's why I posted question. I'm interesting to read why he change it. i think there is no particular reason.
    Ok, thanks for answering. It's really nice solution.
    Thanks

    - Micko
    Last edited by Micko; 11-07-2005 at 04:28 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM