• C

Strtok function

If anyone writes this before Saturday and it passes my main.c function test (can be requested) it’s worth 500 points.

The motivation behind this program is to implement the functionality of the tokenizer that is a part of all compilers, interpreters, parsers, and scripting languages. For example, the C parser identifies the following comma-separated list of tokens (which are the smallest elements of a C program that are meaningful to the compiler) from the expression a=i+++j: a, =, i, ++, +, j. For our program, we will use the tokenizer to interpret the contents of a null-terminated string as words by filtering out the non-alphabetical characters. The following is a description of the function that you must implement:
Write a function StringToken() with the following prototype:
char* StringToken (char *str, const char *set);
StringToken mimics the behavior of function strtok declared in the C standard library file string.h. The explanation for StringToken is a function that searches str for tokens delimited by characters from set. A sequence of calls of StringToken(str, set) splits str into tokens, each delimited by a character from set. The first call in a sequence has a non-null str. It finds the first token in str consisting of characters not in set; it terminates that by overwriting the next character of str with ‘\0’ and returns a pointer to the token. Each subsequent call, indicates by a null value of str, returns the next such token, searching from just past the end of the previous one. StringToken returns null when no further token is found. The string set may be different on each call.
Sample Program:
Here is a sample program that illustrates the use of StringToken to determine the number of words in a null-terminated string by filtering out certain non-alphabetical characters:
#include <stdio.h>
#define LINE_LEN            1024
char * StringToken(char *str, const char *set)
            /* … */
int main(void)
      Note the use of the escape sequence \" to represent double quotation marks within
the string. An escape sequence begins with the backslash '\' and is used to represent certain nonprintable characters ( for example: '\0', '\a' (bell)),
the single and double quotation marks and the backslash characters ('\'', '\"', '\\').
            char line[LINE_LEN] = “\”My goodness,\” she said, \”Is that right?\””;
            /* Set of non-alphabetic characters */
char set[] = “ .,?!’\”\t\n”;
            char *word = StringToken(line, set); /* find first word */
            while (word)
                  printf("<%s>\n", word);
                  word = StringToken(0, set); /* find next word */
            return 0;
The results of the above program are the words (delimited by <>):
The function StringToken requires a variable defined with the storage-class specifier static. Block-scope variables declared with the static storage class specifier have static extent (as opposed to local extent, signified by auto). An object is said to have static extent when it is allocated storage at or before the beginning of program execution and the storage remains allocated until program termination. Consider the following example:
#include <stdio.h>
      void foo(int *p)
            static int k = 5;
            *p += k;
      int main(void)
            int k = 0, *p = &k;
            printf(“k = %d\n”, k);
            printf(“k = %d\n”, k);
            printf(“k = %d\n”, k);
The three values displayed by the program would be 6, 13, and 21, respectively. Note that object k with block-scope in function foo has static extent and is initialized with the integer value 5 at or before the beginning of program execution
Your submission would consist of two files: a C source file and a header file.
Your submission must not use any global variables.
 No Standard C Library functions are to be used in your submission
Ensure that there are no statements that perform I/O.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sorry postal21 ... EE membership agreement forbids us from doing your homework ...
You have to do it on your own ...
If you are stuck with a specific problem, we can give you a push but we cannot do it for you
that been said, perhaps you may find strcspn() function useful

man strcspn for more details
We can't do the whole thing, but here's a  outline.
Note , several boundary conditions and off-by-ones left in as
an exercise. :)

char * p = NULL;

char* StringToken (char *str, const char *set)
if( str != NULL ) p = str;

Start = p;
while( Isin( *p++, set ) ) p++;
*p = '\0';
return( Start );

int Isin( char Target, cha * List )
int Found;
Found = 0;
while( ! Found && *List != '\0' ) Found = *List++ == Target
return( Found );


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.