CSE 1311 - Section 2 - Assignments - Assignment 7

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: 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:


Task 2 (25 points)

Write a program called task2.c or task2.cpp, that behaves as follows: 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:


Task 3 (25 points)

Write a program, in a file called task3.c or task3.cpp, that behaves as follows:

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:


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:

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