Link to home
Start Free TrialLog in
Avatar of postal21
postal21

asked on

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 <>):
 
<My>
<goodness>
<she>
<said>
<Is>
<that>
<right>
 
 
Hint:
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;
 
            k++;
            *p += k;
      }
 
      int main(void)
      {
            int k = 0, *p = &k;
 
            foo(p);
            printf(“k = %d\n”, k);
 
            foo(p);
            printf(“k = %d\n”, k);
 
            foo(p);
            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.
Avatar of sunnycoder
sunnycoder
Flag of India image

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
ASKER CERTIFIED SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial