For this lab, you will use the following code snippet and fill in the
function bodies for reverse_index
and
reverse_pointer
. You can also view this code seperately in
lab2_handout.cpp and you can copy this code
to your current directory using
cp /usr/users/mdanfor/public_html/cs222-w07/lab2_handout.cpp .Here is the code snippet:
#include <stdio.h> #include <iostream> using namespace std; // This recursive function uses array subscripts to print the string in reverse // Input: a character array that terminates with '\0' // an integer indicating the current character to inspect // Output: the string in reverse void reverse_index(char s[], int start); // 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 array subscripts: "); reverse_index(str, 0); printf("\n"); printf("Reverse using pointers: "); reverse_pointer(str); printf("\n"); return 0; }The pseudocode for the recursive function as discussed in class is:
if current character is the null character return, do nothing (base case) else call reverse with the next character in the string print the current character