Null-Safe Command Tokenizer

Medium C Robustness Asked at Zoho
Solve this problem

Split a command string into space-separated tokens in place, without allocating and without walking off the end of the buffer when the input is malformed.

Constraints

  • Signature: `int tokenize(char *line, char **argv, int max_tokens)` returning the token count.
  • Tokens are separated by one or more spaces; leading and trailing spaces are ignored.
  • Modifies `line` in place by writing terminators.
  • Must stop at max_tokens without overrunning argv.
  • A NULL or empty line returns 0.
  • No strtok, no malloc.
Starter codeC
1int tokenize(char *line, char **argv, int max_tokens) {2    // TODO3    return 0;4}