Reverse String Assignment

For this assignment you will write 2 functions which reverse strings in different ways.

Code Structure

You have been provided with all the files you need for this assignment. There is a Makefile provided so you can compile the project by running make. main.c contains code to test the functions you will write. The prototypes and documentation for the functions are in reverse.h. You are to implement the functions in reverse.c.

Before you begin, try compiling the program with make and running it with ./reverse. As it is, the program will ask you to enter a word and then it prints out the word along with its length. It uses the C standard library function strlen() to find the length of the string. Remember, the length of a string is the number of characters before the first '\0' in the array containing the string, not the number of elements in the array itself. Read main.c to see how this is accomplished.

You will now write 2 functions which reverse the string that was entered in two different ways. One function creates a reverse of the string in a second array, and the other reverses the string by modifying the original array. There are commented out lines of code in main.c that test these two functions. Uncomment these lines once the functions are written to test them.

reverse_string()

Here is the prototype for reverse_string():

/**
 * Create the reverse of a string inside another array.
 *
 * Parameters:
 *   original - the original string, which will remain unchanged
 *   reversed - another character array which will store the reversed string
 *
 * Return value
 *   Nothing, because the reversed string is stored in the provided array
 */
void reverse_string(const char original[], char reversed[]);

This function must reverse the string in original and store the reverse of it in reversed. original will remain unchanged. You can use the strlen() function to determine the length of the original string.

In your loop you will have to store each character of the original string in the correct index in the reversed string. For example, if you are reversing Hello, then the 'H' must be stored at index 4 in reversed, the 'e' must be stored at index 3, etc.

Remember you must place a '\0' at the appropriate position in reversed before the function returns.

The function does not need to return a value because it modifies whatever array is passed in as reversed.

Once you have written this function, uncomment the lines in main.c which test it.

reverse_string_in_place()

Prototype:

/**
 * Reverse a string in place.
 *
 * Parameters:
 *   string - the string to reverse
 *   
 * Return value
 *   Nothing, because the string itself is changed
 */
void reverse_string_in_place(char string[]);

By “in place” we mean that we are not placing the reverse of the string in a separate array, we are swapping the characters within the original string so that it becomes a reverse of its original self.

To swap two elements in an array you have to use a temporary variable. For example, if we want to swap the element at index i with the element at index j in an array called string, we cannot do this:

string[i] = string[j];
string[j] = string[i];

If we do that, both elements will end up being the value that was originally in string[j], since the original value of string[i] is lost after the first assignment.

Instead, we need to save the value that was in string[i] in a temporary variable and then set the new values, like so:

char temp = string[i];
string[i] = string[j];
string[j] = temp;

You’ll have to think about how to do this with a loop. For the string Hello you need to swap index 0 with index 4, and then swap index 1 with index 3.

Once you have written this function, uncomment the lines in main.c which test it.

Submission

Push your code back to git-keeper when you are finished. You will be graded on both correctness and style.