Lab 9 - Pointers and Functions

The purpose of this lab is to write functions that compare pointer arithmetic to using array subscripts. Name your file lab9.cpp.

For this lab, you will use the following code snippet and code the function bodies for the functions convert_index and convert_pointer. You can also view this code seperately in lab9_handout.cpp. You can copy this code to your Sleipnir account using vi set paste command for copy/paste, using wget to download from the website or using cp to copy from my directory.

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

// Input: A character array that terminates with '\0'
//        An integer indicating the first index to convert
//        An integer indicating the index one after the last index to convert
// Output: Converts the lowercase characters between the indices to uppercase
void convert_index(char s[], int start, int end);

// Input: A pointer to the first character to convert
//        A pointer to the character one after the last character to convert
// Output: Converts the lowercase characters between the pointers to uppercase
void convert_pointer(char *start, char *end);

int main()
{
  char str[51];
 
  // This is the non-depreciated way to initialize a C-string with a string
  // literal, instead of using array initialization.
  strncpy(str, "This is a test.", 50);

  cout << "Original string: " << str << endl << endl;

  convert_index(str, 0, 5);
  cout << "After index convert: " << str << endl << endl;

  convert_pointer((str+5), (str+10));
  cout << "After pointer convert: " << str << endl << endl;

  return 0;
}
Both functions will use a for() loop to check if the character is lowercase and, if so, convert it to upper case.

The convert_index function will use the subscript operator (e.g. s[i]) to access each element. It takes a character array for the string, an integer for the first index to use and an integer for the last index. The for() loop should start at the first index (e.g. int i = start), and keep going while i is less than the last index. The last index is not inclusive.

The convert_pointer function will use pointers to access each element. It takes a character pointer for the address of the first character and a character pointer for the address immediately after the last character. The for() loop should start at the first character (e.g. char *p = start) and keep going while p is less than the address after the last character. The ending address is not inclusive, which is why it is the address immediately after the last character.

There should be no cin or cout statements inside your function bodies. The main function should also remain unchanged. All you will add is the function bodies below main.

Email the completed lab9.cpp to me on Sleipnir.