Question

In: Computer Science

Write a modified version of the program below. In this version, you will dynamically allocate memory...

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;

}

Solutions

Expert Solution

#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;

}


Related Solutions

Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory. Help ASAP!!!
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first n prime numbers. For example, P(1) = 2 and P(3) = 10, since the first three prime numbers are 2, 3 and 5, respectively. Write a program to determine the value of the function P(n) for different values of n. The first few prime sums are 2, 5, 10, 17, 28, 41, 58 and 77. Input The first line of the input file contains...
Write down Maxwell’s Equations (integral notation). Write down a modified version of Maxwell’s Equations that includes...
Write down Maxwell’s Equations (integral notation). Write down a modified version of Maxwell’s Equations that includes magnetic monopoles. Use the symbol qm for magnetic change and J for magnetic current. Hint: Think of symmetry and units.
Write a Python Version 3 program that will ask you the loan amount and the interest...
Write a Python Version 3 program that will ask you the loan amount and the interest rate. Then the program will tell you - The monthly payment, How many months it will take you to pay off the loan, The total amount of interest paid over the life of the loan. Program should also indicate the final payment as in many cases the final payment at the end of the loan would not be exactly the normal monthly payment and...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT