How to count Words in a string?
-
Please, if somebody has ready solution (function for counting of words in a string), write it here. I just want to obtain number of words in LPSTR string. Thanks!
-
Please, if somebody has ready solution (function for counting of words in a string), write it here. I just want to obtain number of words in LPSTR string. Thanks!
You can use strtok. First define a string of separators and then use strtok to find all matching tokens and count them. Best regards, Alexandru Savescu
-
You can use strtok. First define a string of separators and then use strtok to find all matching tokens and count them. Best regards, Alexandru Savescu
Thanks! Here was example: * STRTOK.C: In this program, a loop uses strtok * to print all the tokens (separated by commas * or blanks) in the string named "string". */ #include #include char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; void main( void ) { printf( "%s\n\nTokens:\n", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %s\n", token ); /* Get next token: */ token = strtok( NULL, seps ); } } Output A string of ,,tokens and some more tokens Tokens: A string of tokens and some more tokens