Parsing input in C is initially more difficult than in other, higher level, languages. However, the C library includes a wide variety of functions that make input handling straightforward after you've spent a little time in practice.
In this studio, you will:
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your work by sending your text file and source code to firstname.lastname@example.org with the phrase String Input in the subject line.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
See the man page for
fgets() for more detail. You will need to
declare a sufficiently large
char buffer to store user input,
and you should read from the standard input stream ('
Copy and paste the output of your program once it runs correctly.
strtok(), which parses a string from left to right and returns one token each time you call the function. Pull up the
strtok()manual page and read the first paragraph. What difference is there between the initial call to
strtok()and subsequent calls that parse the same string?
strtok()function which character marks the boundary of tokens. What delimiter character should you use for regular text input?
strtok()so that your program parses the first token from the user input. Print this first token on a new line.
Copy and paste your program output once this works correctly.
This is an input string.
The actual string recieved by your program is:
"This is an input string.\n"
We want to strip this trailing newline character, as it will cause
problems for us later. There are a variety of approaches to doing this, such
as using the
strchr() functions, or even
strtok() function itself. Modify your program to strip the
newline character after your call to
fgets(), but before you start
parsing. Try a Google search for "strip newline character in C" if you would
like some further inspiration.
As the answer to this exercise, copy and paste your newline stripping code.
"ls -l"then your program should execute the command
ls -l. We will use the function
execvp() function takes two arguments. First, a
that contains the command we want to execute (e.g.
above). Second, a
char* argv structure.
Declare the following variables and leave this answer blank.
int max_args = 15;
int max_argv_size = max_args + 2; //one for argv, one for null terminator
cmdvariable is easy. This will always be the first token returned from
strtok(). Note that
char*and this is exactly the type we have declared for
Copy and paste your assignment statement as the answer to this exercise.
my_argvis slightly trickier. First, observe that
my_argvis simply an array of
char*pointers. Second, recall that the regular
argvvariable always has the name of the command as the first element (
argv), and is always terminated by a NULL character.
Start by assigning "
my_argv = cmd;". Then, parse the
rest of the user
input string and assign each subsequent token to each subsequent position. For
example, if the result of
strtok() is stored as such:
Then assign each token thusly, for an appropriate value of
res = strtok(NULL, " ");
my_argv[i] = res;
IMPORTANT: After there are no more tokens to parse, assign the next element of
my_argv the NULL character:
my_argv[after_last] = '\0';
Copy and paste your
my_argv code as the answer.
my_argv, insert the following call to
execvp( cmd, my_argv );
If the two variables are constructed correctly, your program should read
the user input, parse it as though it were a new program to execute, and then
execute that program with
Copy and paste a working output as the answer to this question.