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