Computer Systems Organization I - Prof. Grishman

Assignment #5

Write a C function gettoken which reads from standard input a string containing the next sequence of non-whitespace characters.  That is, it skips any leading whitespace characters, and then returns (as a zero-terminated string) the following characters up to (but not including) the next whitespace character.  The whitespace characters include the blank (' '), newline ('\n'), return ('\r'), and tab ('\t').  If no non-whitespace characters remain, it should return NULL.

Successive invocations of gettoken should return distinct strings ... distinct areas of the heap.  (Use the getline function presented in class today as a model.)

Write a main function which reads in all the tokens from standard input and then -- after all tokens are read -- prints them, one token per line, to standard output.

For 0.5 points of extra credit, sort the tokens (into ascending alphabetical order) before printing them.  Any simple sort routine (bubble sort, insertion sort, etc.) will be fine.  You may use the library function strcmp to compare strings (see page 613 of the text for the spec of this function).

Create a program file consisting of the main and gettoken functions, and name it lastname5.c (for example, my file would be grishman5.c).  Submit your program (.c file) by email, as an attachment, to the e-tutor, Ian Lau <>, by one minute before midnight on Monday, November 15th. (Late assignments will be penalized 1/2 point for each day late, out of a total of 5 points.) Label your email "CSO Asgn 5".