Hi.
I am using:
What do you think? Is this gonna be okay?Code:int retcount (char * text) { char *tmp = text; int count = 0 while (strtok(tmp, " ")) count++; return count; }
Hi.
I am using:
What do you think? Is this gonna be okay?Code:int retcount (char * text) { char *tmp = text; int count = 0 while (strtok(tmp, " ")) count++; return count; }
Why don't you just keep it simple?Of course you could do...Code:for( count = 0; text; text++ ) { if( *test == ' ' ) count++; }Or maybe...Code:for( count = 0; strchr( text ); text++, count++ );That'll get you started.Code:int count( char *s ) { return s ? *s == ' ' ? 1 + count( s + 1 ) : 0 + count( s + 1 ) : 0; }
Quzah.
Hope is the first step on the road to disappointment.
Code:for( count = 0; text; text++ ) /* probably *text ? */ for( count = 0; strchr( text ); text++, count++ ); /* strchr() wrong usage or pseudo code?*/
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.
I was tired and on the way to bed.And...Code:for( count = 0; *text; text++ ) { if( *test == ' ' ) count++; }For some reason I was thinking I was updating text. Don't think while tired.Code:for( count = 0; strchr( text, ' ' ); text += 1+(strchr( text, ' ' )-text), count++ );1. Tired is bad.Code:int count( char *s ) { return s && *s ? *s == ' ' ? 1 + count( s + 1 ) : 0 + count( s + 1 ) : 0; }
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.