Write a program to allocate memory dynamically for a sentence to enter by user, ask user to know the no of character in his/her sentence



Here we will discuss how we can allocate memory and for that we will be using here <
iomanip> and here we will allocate it using cp pointer. Once we done we will clear the memory for that we will use delete function.

 /*Write a program to allocate memory dynamically for a sentence to enter by user, ask user to know the no of character in his/her sentence*/

#include<iostream>

#include<iomanip>

using namespace std;

int main(){

int n;

cout<<"How many character there are in your sentence?";

cin>>n;

char *cp= new char[n]; //dynamically memory allocation

cout<<"Enter your Sentence:";

cin.get(cp,n,'.');

cout<<"Entered sentence is"<<cp;

delete cp;// Memory Freeup

cout<<endl<<"after deallocating memory:"<<cp;

return 0;

}



0 Comments had been done yet:

Post a Comment