// Saliha Babar CS1A Chapter 10, #2, Page 588
//
/*****************************************************************************
* REVERSE STRING
* ____________________________________________________________________________
* This program accept strings up to n characters, and it will reverse the
* string.
*
* Reverse happens as the following
* "Saliha" will result in "ahilaS".
* ______________________________________________________________________________
* INPUT
* size : size of the array allocated for string
* word : string of n characters
*
* OUTPUT
* reverseArray : pointer for the reversed array
* ****************************************************************************/
#include <iostream>
#include <cstring>
using namespace std;
char* BackwardString (char array[], int size);
int main() {
const int SIZE = 51; // INPUT - size of the string
char word[SIZE]; // INPUT - array of the characters
int strLength; // Length of string
char* reverseArray; // Pointer of reversed array
cout << "Enter any word up to " << SIZE - 1 << " characters long\n";
cin.getline(word, SIZE);
cout <<"This is original string \"" << word << "\"\n";
// Check for printable characters
strLength = strlen(word);
// Call the backward function and reverse the array
reverseArray = BackwardString (word, strLength);
cout <<"This is reversed string \"" << reverseArray << "\"\n";
delete [] reverseArray;
return 0;
}
char* BackwardString (char array[], int size)
{
// Create a new memory location for reversed array
char* reverse;
reverse = new char [size+1];
for (int i = 0; i < size ; i++)
{
reverse [size -1-i] = array[i] ;
}
reverse[size] = '\0';
return reverse;
}