Question

part8.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> /* In part8.c, you will implement a...

part8.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* In part8.c, you will implement a function, called string_token, that
* splits a string into a sequence of tokens according to
* a specific delimiter character.
*
* On the first call, the string to be parsed is given in the "str" argument. The "saveptr"
* argument is ignored. The function returns "saveptr", which is to be used internally by string_token() in
* subsequent calls that parse the same string in order to maintain context.
*
* On a subsequent call to parse the same string, the caller is expected to set the
* "str" argument to be NULL, and set "saveptr" to be the returned saveptr value of
* the previous string_token invocation.
*
* Each call to string_token returns a pointer to a null-terminated string containing the next token.
* This string does not include the delimiting byte. More concretely, the next token is determined by
* scanning forward (from either "savedptr" or from "str" depending on whether it's the first or subsequent string_token call)
* for the next * non-delimiter character. If no such character is found, the function returns NULL.
*
* Here's an example usage of the function string_token:
*
* char str[100] = "aaa;;bbb;ccc"
* char *savedptr;
*
* while (1) {
*    token = * string_token(str, ';', &savedptr);
*    if (token == NULL) {
*       break;
*    }
*    printf("%s\n"); str = NULL; // set str to be NULL for subsequent string_token call
* }
*
* The above code snippet should print out 3 lines:
* aaa
* bbb
* ccc
*
*
* Note: The string_token function is supposed to modify the buffer holding the
* argument "str". It should not perform any memory allocation (e.g. using
* malloc).
*
*
* Note: This function is similar but not identical to the standard C library's
* strtok_r(...) function.
*/

char *
string_token(char *str, char delim, char **saveptr)
{
   //TODO: Your code here
}

part8.h

#pragma once

char *string_token(char *str, char delim, char **saveptr);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>


char* string_token(char *str, char delim, char **saveptr)
{
char *remaining;

if (str == NULL)
str = *saveptr;

if (str == NULL)
{
*saveptr = str;
return NULL;
}

char *delim_ptr = strchr(str, delim);

if(delim_ptr == NULL)
{
remaining = NULL;
}
else
{
remaining = delim_ptr + 1; //contains the string after the first occurrence of delimiter
*delim_ptr = '\0'; //Now str will contain string before delimiter
}

if ( remaining == NULL || *remaining == '\0' )
{
*saveptr = remaining;
return str;
}

*saveptr = remaining; //saveptr contains the remaining part
return str;
}

int main()
{
char p[100] = "aaa;;bbb;ccc";
char *str = p;
char *savedptr;
while (1) {
char *token = string_token(str, ';', &savedptr);
if (token == NULL) {
break;
}
printf("%s\n", token);
str = NULL; // set str to be NULL for subsequent string_token call
}
}

Add a comment
Know the answer?
Add Answer to:
part8.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> /* In part8.c, you will implement a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #pragma warning(disable : 4996) // to avoid scanf...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #pragma warning(disable : 4996) // to avoid scanf error, Visual Studio only #define MAX_DIM 3 #define STUDENTS 5            // total number of students int scores[STUDENTS]; int *pScores = NULL; C language Implement the function that creates and displays the user name of the user. Ask the user for their first and last name. The username is the first letter of first name, followed by the last name. For instance, if the user's...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

  • Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n...

    Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT