Due date: Wednesday, March 10, 2010, 11:55pm.
Task 1 (20 points)
Write a program called task1.c or task1.cpp, that behaves as follows:
- It asks the user to enter a string A and an integer B.
- If B < 1, the program exits without doing anything more. For the next steps, we assume that B >= 1.
- It creates a new string C, obtained by repeating B times every letter of A.
- It prints out the new string C.
For full credit, you should implement a function ... repeat_letters(... A, ... B), that takes as input a string A and an integer B, and returns a new string C, obtained by repeating B times every letter of A.
Examples:
- repeat_letters("abc", 2) should return string "aabbcc".
- repeat_letters("a", 5) should return string "aaaaa".
- repeat_letters("xy", 1) should return string "xy".
- repeat_letters("to", 3) should return string "tttooo".
Task 2 (20 points)
Write a program called task2.c or task2.cpp, that behaves as follows:
- It asks the user to enter a string A.
- It creates a new string C, that is like A, with an extra period ('.' character) at the end.
- It prints out the new string C.
For full credit, you should implement a function ... put_dot(... A), that takes as input a string A, and returns a new string C, that is like A, with an extra period at the end.
Examples:
- put_dot("abc") should return string "abc.".
- put_dot("..") should return string "...".
- put_dot("xy.") should return string "xy..".
- put_dot("to") should return string "to.".
Task 3 (20 points)
Write a program, in a file called task3.c or task3.cpp, that behaves as follows:
- It asks the user to enter a string A.
- It creates a new string C, that is like A, with a period inserted after each character of A.
- It prints out the new string C.
For full credit, you should implement a function ... put_dots(... A), that takes as input a string A, and returns a new string C, that is like A, with a period inserted after each character of A.
Examples:
- put_dots("abc") should return string "a.b.c.".
- put_dots("..") should return string "....".
- put_dots("xy.") should return string "x.y...".
- put_dots("to") should return string "t.o.".
Task 4 (20 points)
Mr. Bugs wants to write a simple program that creates a string "Hello" and prints it. Unfortunately, the program does not print the string correctly. Can you find and correct the problem? Note that it is not sufficient to provide code that behaves correctly, you must specify exactly what was wrong with Mr. Bugs' code. Include the answer in your answers.doc file.
Here is the program that Mr. Bugs wrote:
#include <stdio.h>
int main()
{
char * my_string = (char *) malloc(sizeof(char) * 5);
my_string[0] = 'h';
my_string[1] = 'e';
my_string[2] = 'l';
my_string[3] = 'l';
my_string[4] = 'o';
printf("my_string = %s\n", my_string);
}
Task 5 (20 points)
Mr. Bugs tried another approach to get the program of task 4 to work correctly. Unfortunately, the new program still does not print the string correctly. Can you find and correct the problem? Note that it is not sufficient to provide code that behaves correctly, you must specify exactly what was wrong with Mr. Bugs' code.
Here is the program that Mr. Bugs wrote:
#include <stdio.h>
int main()
{
char * my_string = (char *) malloc(sizeof(char) * 6);
my_string[0] = 'h';
my_string[1] = 'e';
my_string[2] = 'l';
my_string[3] = 'l';
my_string[4] = 'o';
my_string[5] = '0';
printf("my_string = %s\n", my_string);
}
How to submit
Submisions are only accepted via WebCT. The submission should include your C or C++ source files, and answers.doc. Do not submit entire project directories, or anything else except for the .c and .cpp files and the answers.doc file.
Your answers.doc file should be a single Microsoft Word file that includes:
- Your name and UTA ID number at the top.
- A copy of your code for each task where you had to write code, and your answer for tasks where you had to provide an answer.
Make sure that your answers.doc file is easy to parse, and that it is easy for us to figure out which part corresponds to which task. Feel free to use headings such as "start of copy of task1.c".
We try to automate the grading process as much as possible. Not complying precisely with instructions causes a significant waste of time during grading, and thus points will be taken off for failure to comply, and/or you may receive a request to resubmit.
Submission checklist
- Was the assignment submitted via WebCT?
- Did answers.doc contain your name and UTA ID?
- Did the submission include files your C or C++ source files, answers.docs, and nothing else?