In: Computer Science
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings). Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory!
#include <iostream>
#include <cstring>
using namespace std;
int Reverse(char * destination, const char * source, int num);
int main() // this is the test/driver code, for your function
{
const int STRINGSIZE = 10;
char oldCString[] = "Hello!";
char newCString[STRINGSIZE];
cout << "oldCString: " << oldCString << endl;
cout << "newCString before changing: " << oldCString << endl;
Reverse(newCString, oldCString, STRINGSIZE); // testing your function...
cout << "newCString after Reverse: " << newCString << endl;
return 0;
}
// Reverses a C-string passed in (source), and places the reversed
// C-string into (destination).
// (num) should represent the maximum valid length of (destination).
// If no null-zero character is found in (source) within (num-1)
// characters,the function will only read up to (num-1) characters
// from (source), and then copy the reversed characters to
// (destination) and append a null-zero to the end of it.
// The function will return the number of characters placed into
// (destination), including the null-zero.
// The function MUST use pointer notation (not array notation) inside
// it. However, you might find it useful to use array notation
// temporarily while developing the function, and then replace
// with pointer notation before turning in the assignment.
int Reverse(char * destination, const char * source, int num)
{
for (int x = 0; x < strlen(source); x++)
*( x + destination) = *( strlen(source) + source - 1 - x);
*(destination + strlen(source)) = '\0';
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int Reverse(char * destination, const char * source, int num);
int main() // this is the test/driver code, for your function
{
int STRINGSIZE = 0;
cout<<"Enter size of C-string : ";
//ask user for string size
cin>>STRINGSIZE;
char *oldCString = new char[STRINGSIZE]; //dynamic allocation of string using new keyword
cout<<"Enter a string : ";
//ask user to enter a string of STRINGSIZE entered previously
cin>>oldCString;
char *newCString = new char[STRINGSIZE]; //dynamic allocation of string using new keyword
cout << "oldCString: " << oldCString << endl;
cout << "newCString before changing: " << oldCString << endl;
Reverse(newCString, oldCString, STRINGSIZE); // testing your function...
cout << "newCString after Reverse: " << newCString << endl;
delete(oldCString); //deleting the memory allocated to
oldCString
delete(newCString); //deleting the memory allocated to
newCstring
return 0;
}
// Reverses a C-string passed in (source), and places the reversed
// C-string into (destination).
// (num) should represent the maximum valid length of (destination).
// If no null-zero character is found in (source) within (num-1)
// characters,the function will only read up to (num-1) characters
// from (source), and then copy the reversed characters to
// (destination) and append a null-zero to the end of it.
// The function will return the number of characters placed into
// (destination), including the null-zero.
// The function MUST use pointer notation (not array notation) inside
// it. However, you might find it useful to use array notation
// temporarily while developing the function, and then replace
// with pointer notation before turning in the assignment.
int Reverse(char * destination, const char * source, int num)
{
for (int x = 0; x < strlen(source); x++)
*( x + destination) = *( strlen(source) + source - 1 - x);
*(destination + strlen(source)) = '\0';
return 0;
}