I want to write a program in C or C++ that asks to user to enter the string at run time of different lengths at different times as given by user (either space separated or non space separated) and store it into an array. Please give me the sample code in C and C++ for it.
e.g.
1st run:
Enter string
Input: Foo
Now char array[]="foo";
2nd run:
Enter string
Input:
Pool Of
Now char array[]="Pool Of";
I have tried:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"enter no. of chars in string";
cin>>n;
char *p=new char[n+1];
cout<<"enter the string"<<endl;
cin>>p;
cout<<p<<endl;
cout<<p;
return 0;
}
But it's not working when the string is space separated.
I have tried this too, but it's not working either.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"enter no. of chars in string";
cin>>n;
char *p=new char[n+1];
cout<<"enter the string"<<endl;
cin.getline(p,n);
cout<<p<<endl;
cout<<p;
return 0;
}