//Sam Partovi CS1A Ch. 10, P.588, #2
/*******************************************************************************
* CREATE REVERSED STRINGS
* ____________________________________________________________
* This program accepts a given string and reverses the order of the characters,
* and displays the contents of the string in reverse order.
* ____________________________________________________________
* INPUT
* ARRAY_SIZE: Size of string array
* string: Given string to reverse
* OUTPUT
* The string variable's contents in reversed order
*******************************************************************************/
#include <iostream>
#include <cstring>
using namespace std;
//Function prototype for ReverseStringFunc
char *ReverseStringFunc(char string[]);
int main() {
const int ARRAY_SIZE = 101; //INPUT - Size of string array
char string[ARRAY_SIZE]; //INPUT - Given string to reverse
//Prompt for given string
cout << "Enter a string up to " << ARRAY_SIZE - 1 << " characters: ";
cin.getline(string, ARRAY_SIZE);
//Call ReverseStringFunc function and assign returned value to string
ReverseStringFunc(string);
//Display the reversed string
cout << "\n" << string;
return 0;
}
//******************************************************************************
//Function definition for ReverseStringFunc
// This function accepts a given string and returns it in reversed order.
//*****************************************************************************/
char* ReverseStringFunc(char string[]) {
int length = strlen(string); //Obtain string length
char temp; //Temporary variable for swap
//Reverse the string
for (int i = 0; i < length / 2; i++) {
temp = string[i];
string[i] = string[length - i - 1];
string[length - i - 1] = temp;
}
return string; //Return the reversed string
}