Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Submission ----------- Please refer to the lab retrieval and submission instruction, which outlines the only way to submit your lab assignments. Please do not email
Submission -----------
Please refer to the lab retrieval and submission instruction, which outlines the only way to submit your lab assignments. Please do not email me your code.
If a lab assignment consists of multiple parts, your code for each part must be in a subdirectory (named "part1", "part2", etc.)
Please make sure that your submission satisfies the requirements for the following items:
- README.txt - Makefile
The requirements remain the same as lab 1.
Valgrind ---------
There is another requirement that applies to all labs starting lab 2.
You will be heavily penalized if your program contains memory errors. Memory errors include (among other things) failure to call free() on the memory you obtained through malloc(), accessing past array bounds, dereferencing uninitialized pointers, etc.
You can use a debugging tool called "valgrind" to check your program: valgrind --leak-check=yes ./your_executable
It will tell you if your program has any memory error. See "The Valgrind Quick Start Guide" at http://valgrind.org/docs/manual/quick-start.html for more info.
You must include the output of the valgrind run for EACH PART in your README.txt. In addition, TAs will run valgrind on your program when grading.
You can include the valgrind output in your README.txt as follows. If youre in your part1 directory, for example, issue the following command:
valgrind --leak-check=yes ./your_program >> ../README.txt 2>&1
The command will append the valgrind output at the end of your README.txt. Make sure you put ">>", not ">". If you type ">", it will overwrite your README.txt.
Echo with a twist --------------------------------------
Write a program, named "twecho", that takes words as command line arguments, and prints each word twice, once as is and once all-capitalized, separated by a space. For example,
./twecho hello world dude should output:
hello HELLO world WORLD dude DUDE
Your program should handle any number of arguments. You can receive the command line arguments if you start your main() function in the following way:
int main(int argc, char **argv)
Please refer to section 5.10 in K&R2. In particular, the picture on page 115 depicts clearly how the command line argument strings are stored in memory.
Here are some requirements and hints:
- You must use the main() function exactly as given below. You CANNOT modify the main function. Your job is to implement other functions that main() calls.
int main(int argc, char **argv) {
if (argc <= 1) return 1;
char **copy = duplicateArgs(argc, argv); char **p = copy;
argv++; p++; while (*argv) {
printf("%s %s ", *argv++, *p++); }
freeDuplicatedArgs(copy);
return 0; }
- You will probably need the following #includes:
#include#include #include #include
- You can put duplicateArgs() and freeDuplicatedArgs() in the same .c file as main().
- In duplicateArgs() function, you are making a "copy" of the memory structure shown in the picture on page 115, K&R2. You will call malloc() once for the overall array where each element is of type char*, then you will call malloc() for each element of that array, each of which will hold the all-cap version of each argument. Of course, you will have to copy each string character-by-character, capitalizing as you go.
Some useful library functions for doing this include strlen() and toupper(). See the textbook.
Dont forget that the last element of the overall array of char*s is a NULL pointer (see the picture in page 115, K&R2).
- In freeDuplicatedArgs() function, you must free() everything you malloc()ed. First free() all individual strings, and then free() the overall array.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started