Due date: Wednesday, March 24, 2010, 11:55pm.
Task 1 (25 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 the entire string A.
- It prints out the new string C.
For full credit, you should implement a function ... repeat_string(... A, ... B), that takes as input a string A and an integer B, and returns a new string C, obtained by repeating B times the entire string A.
Examples:
- repeat_string("abc", 2) should return string "abcabc".
- repeat_string("a", 5) should return string "aaaaa".
- repeat_string("xy", 1) should return string "xy".
- repeat_string("to", 3) should return string "tototo".
Task 2 (25 points)
Write a program called task2.c or task2.cpp, that behaves as follows:
- It asks the user to enter two strings, A and B.
- It creates a new string C, that is the concatenation of A and B.
- It prints out the new string C.
For full credit, you should implement a function ... concatenate(... A, ... B), that takes as input two strings A and B, and returns a new string C, that is the concatenation of A and B.
Examples:
- concatenate("abc", "fed") should return string "abcfed".
- concatenate("a", "dog") should return string "adog".
- concatenate("xy.", "") should return string "xy.".
- concatenate("to", "morrow") should return string "tomorrow".
Task 3 (25 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 prints out the number of vowels (letters 'a', 'e', 'i', 'o', u') in A.
For full credit, you should implement a function ... count_vowels(... A), that takes as input a string A, and the number of vowels (letters 'a', 'e', 'i', 'o', u') in A.
Examples:
- count_vowels"abcabe") should return 3.
- count_vowels("..") should return 0.
- count_vowels("xy.") should return 0.
- count_vowels("university") should return 4.
Task 4 (25 points)
Mr. Bugs wants to write a simple program that asks the user to enter a string, and modifies the string to insert a questionmark (character '?') at the beginning, and prints out the result. To Mr. Bugs' great surprise, the program is not working correctly.
The code fairy visited Mr. Bugs in his sleep, and told him that his main function has three mistakes. Which are those mistakes, and how would you correct them?
Here is the program that Mr. Bugs wrote:
#include <stdio.h>
#include <stdlib.h>
int string_length(char * my_string)
{
int counter = 0;
while(1)
{
if (my_string[counter] != 0)
{
counter++;
}
else
{
break;
}
}
return counter;
}
int main()
{
char * my_string = (char *) malloc(sizeof(char) * 10);
printf("please enter a string:\n");
scanf("%s", &my_string);
int length = string_length(my_string);
char * new_string = (char *) malloc(sizeof(char) * (length + 1));
new_string[0] = '?';
int i;
for (i = 0; i < length; i++)
{
new_string[i+1] = my_string[i];
}
printf("new_string = %s\n", new_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?