C/C++ STRING REVERSALLAB REPORT

1) Enter your name, student ID, platform (Mac or PC) and date

Name and Student ID: Thien Van 0876709

Class: CIS054 C/C++ Programming

Platform (Mac or PC): Mac
Date: 10/28/16

DESCRIPTION:

The project is described in the textbook, Chapter 9 PROGRAMMING PROJECTS #4.

Write a function that uses two pointers, front and rear. Reverse the string by swapping characters pointed to by front and rear. Increment the front pointer and decrement the rear pointer until the entire string has been reversed. Your program will need to determine when the string has been fully reversed and stop swapping characters. Your program must use pointers. Do not submit a program that uses an already available routine that reverses characters in a string.

LAB REPORT:
2) Determine the Inputs, Processing and Outputs before creating the program

INPUTS / PROCESSING / OUTPUTS
string / Copy the string into a character array
Declare front and rear pointers to point to the respective char array element addresses
For loop to swap the element values
Print the new character array
Return the new character array as a string / String (reversed)

3) Fill in the EXPECTEDACTUAL RESULTS

TEST DATA VALUES
Provide examples of a string with an even number of characters and a string with an odd number of characters. / EXPECTED RESULT
Computed values before the program is run / ACTUAL RESULT
Fill in the output displayed
by the program
“this is a string”
“odd”
“even” / gnirts a si siht
ddo
neve / gnirts a si siht
ddo
neve

DISCUSSION:

4) Complete the DISCUSSION section. It does not need to be long, but it needs to be complete.
4a) What did you do to develop the program? ("Followed the Directions" is not a complete description)

Create a template to reverse strings without using pointers using character arrays

Read the lecture notes to understand how pointer and address assignment work

Implement pointer logic

Implement string/char array conversion

4b) What problems did you have and how did you overcome the problems?

Pointer assignments were confusing at first. Had to debug by printing values of pointer and pointer values.

Was not sure if I needed to/how to convert strings to character arrays and vice versa. Had to look up online.

PROGRAM OUTPUT:

5) Show screen shots for at least TWO strings that have been reversed. One string must have an EVEN number of characters and the other string must have an ODD number of characters.

Refer to previous lab assignments for instructions on how to capture a screen or portions of a screen for either the PC or a Mac

PROGRAM LISTING:

6) Copy and paste the code that YOU typed to make the program work. Your program should include a comment block at the top that shows the name of the program, date, version and your name.

#include <iostream>

#include <cstring>

#include <string>

using namespace std;

string reverseString(string str);

int main() {

string input = "this is a string";

string input2 = "odd";

string input3 = "even";

reverseString(input);

reverseString(input2);

reverseString(input3);

}

string reverseString(string str) {

char tmp;

// copy input string into char array

char arr[str.length()];

strcpy(arr, str.c_str());

char *front = &arr[0]; // set front pointer to hold address of beginning of array

char *rear = &arr[str.length()-1]; // set rear pointer...

for (int i = 0; i < str.length()/2; i++){

tmp = *rear; // set tmp char to value of rear pointer

*rear = *front; // set value of rear pointer to value of front pointer

*front = tmp; // set value of front pointer to tmp

rear--; // decrement the rear pointer address

front++; // increment front pointer address

}

for (int i = 0; i < str.length(); i++) {

cout < arr[i];

}

cout < endl;

return string(arr);

}