Write a C program called tokenize.c that tokenizes a line of text into a sequenc
ID: 3777871 • Letter: W
Question
Write a C program called tokenize.c that tokenizes a line of text into a sequence of tokens. A token is a sequence of characters not including a separator. For example, the separator " " and "middot" and ", " and "?" separates a sequence of characters into words. When applied to the text "Hello, I am here." the resulting tokens should be "Hello", "T, "am" and "here". The program implements the function using the following function prototype: char* tokenize (char 'text, const char * separators); The parameter text points to the entire text string, the parameter separators points to a text string where individual characters are considered separators. For example " ., ?" would list the separators used above in a single text string. The function tokenize () may be called several times to extract the tokens of a text string. The function accepts a pointer to a string to search for tokens. Every time a token is identified and a separator character is recognized, the separator is replaced by a "" character to end the token in the string. The function then returns the pointer to the next character in the string that is not a separator. If the '' is recognized as the next character, the function returns the NULL pointer to indicate that no more tokens can be extracted. Note that to read the token, the function must use a pointer to characters that points to the start of a token, while the pointer returned by the function points to the beginning of the next token. For the implementation of the function, you may not use [] to access characters in the array. Instead, you must use pointer arithmetic only to get to the characters in your array. For your test program prompt the user for a line of text, then call tokenize () repeatedly inside a loop and store all returned pointers in an array of pointers to characters of a fixed size. For example, char * tokens [151 creates an array of 15 pointers to characters to store your pointers. Use the array to print all tokens to the screen. Your function returns NULL so that you know you reached the end of the string and you extracted all tokens. The program then terminates. For example, when called for the third time on the input text "Hello. I am here." Your function is given a pointer to the character "a" and returns a pointer to the character "h". Be careful in the implementation of your code because "Hello, , I am here." must still return the words "Hello", "T", "am", "here". Before calling the function, the character array looks like: After calling the function 4 times it looks like:Explanation / Answer
#include <stdio.h>
#include <string.h>
void tokenize(char myStr[] , char mySep[]){
char *pChr = strtok (myStr, mySep);
while (pChr != NULL) {
printf ("%s ", pChr);
pChr = strtok (NULL, mySep);
}
putchar (' ');
}
int main (void) {
char myStr[] = "A,B,C*D?E";
char mySep[] = ",*?";
tokenize(myStr,mySep);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.