These are C Computer Science II programming problem Please
These are C++ ( Computer Science II ) programming problem. Please provide the detailed answer the following problems.
Write a piece of code that takes in a number from the user, creates a char pointer, dynamically allocates a char array to that pointer and reverses the contents of the array. (In between assume the array is loaded with characters) The program should only use bracket notation for allocating memory. Otherwise, pointers should be used.Solution
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
//declaration
int n;
char *aPtr;
//prompt to enter the size of an array
cout<<\"Enter the number:\";
cin>>n;
// allocating char pointer with size n
aPtr =(char*)malloc(n);
//assigning some string
aPtr =\"This is a test string\";
//printing the reverse of an array
for(int i=n-1; i>=0; i--)
cout<<*(aPtr+i);
return 0;
}
OUTPUT:
Enter the number:21
gnirts tset a si sihT
