linux-sysprog · intermediate · ~15 min
Tokenise a command line.
Before a shell can build an argv for exec, it splits the command line into tokens on whitespace. Count those tokens.
Implement int count_args(const char *line) that returns the number of whitespace-separated tokens in line. Treat runs of spaces or tabs as a single separator; ignore leading and trailing whitespace.
line: a command-line string.The number of tokens.
count_args("ls -l /tmp") -> 3
count_args(" echo hi ") -> 2
count_args("") -> 0
line: a command-line string (spaces and tabs separate tokens).
Number of whitespace-separated tokens.
Runs of whitespace collapse; leading/trailing whitespace ignored.
int count_args(const char *line) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.