Question: Write the file parse.c with the the following functions. Include parse.h as the header 1) Write a function int count_tokens( char * str) that gets

Write the file parse.c with the the following functions. Include parse.h as the header

1) Write a function int count_tokens( char * str) that gets a string and counts the number of tokens in str . A token is any substring separated from others by spaces. Examples: On input "ABC ) 123 !!" the function will return 4. On the input "( 8 + ( 41 - 12 ) )" the function will return 9. **You may assume that tokens are separated by a single space, and there are no spaces neither before the first token not after the last token. 2) Write a function char ** get_tokens( char * str, int n_tokens) that gets a string, and the number of tokens obtained in count_tokens , and returns an array of strings, where the ith element of the array contains the ith token. Example: On input str = "( 8 + ( 41 - 12 ) )" and n_tokens = 9 the function returns the array [ "(", "8", "+", "(", "41", "-", "12", ")", ")" ]. ** You may assume that tokens are separated by a single space, and there are no spaces before the first token, and no spaces after the last token. 3) Write a function int is_operator( char * str) that gets a string and checks if it is an arithmetic operator, i.e., is of the four operators "+", "-", "*", "/" . 4) Write a function int is_number( char * str) that checks if a given string is a non-negative integer, e.g., "7" , "16" . 5) Write a function that gets a non-negative int and return a string containing this int. You may assume that the number has at most 2 digits, i.e., is between 0 and 99. char * num_to_str( unsigned int num) * Remember to use malloc to create the returned string of the heap. ** When allocating a string, remember to set the last char to '\0' . In order to convert a digit into a char you may use: int digit = 3 ; char c = '0' + digit;

#ifndef PARSE_H #define PARSE_H

// gets a string a counts the number of tokens in the strings. // The tokens are separated by a single space // There are no spaces before the first token, and no spaces after the last token int count_tokens(char* str);

// gets a string and a number of tokens from count_tokens(str) // returns an array of tokens char** get_tokens(char* str, int n_tokens);

// retuns 1 if str is an operator, i.e., "+", "-", "*", "/" // returns 0 otherwise int is_operator(char* str);

// retuns 1 if str is a number // returns 0 otherwise int is_number(char* str);

// converts a non-negative int to string char* num_to_str(unsigned int num);

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!