Lab 1 - Pointers and Recursion

Due: 5:00pm on Wednesday

The purpose of this lab is to write recursive functions using pointer arithmetic. Name your file lab1.cpp.

For this lab, you will use the following code snippet and fill in the function body for reverse_pointer. You can also view this code seperately in lab1_handout.cpp. To copy and paste this code into a file on Sleipnir when using vi, be sure to turn off indenting using the following command:

:set paste
When you are done pasting, you can turn indenting back on for coding using:
:set nopaste
Here is the code provided in lab1_handout.cpp:
#include <stdio.h>
#include <iostream>
using namespace std;

// This recursive function uses pointer arithmetic to reverse the string
// Input: a pointer to the current character to inspect
// Output: the string in reverse
void reverse_pointer(char *s);

int main()
{
  char str[] = "This is a test.";

  printf("Original string: %s\n", str);

  printf("Reverse using pointers: ");
  reverse_pointer(str);
  printf("\n");

  return 0;
}
The pseudocode for the recursive function is:
if current character is the null character ('\0')
   return, do nothing (base case)
else
   call reverse with the next character in the string
   print the current character
   return out of the function
The reverse_pointer function takes a pointer to the current character in the string as its parameter (char *s). You will be incrementing this pointer with each time you make a recursive function call. You will need to dereference the pointer to compare it to '\0' and to print out the character.

Email the completed lab1.cpp to me on Sleipnir.