I'm trying to create a program that stores words of a string within the space in an array(For example: If user inputs - "Hello what's up" ,So i want to store Hello, What's,Up in an array of thier respective indices (Hello in 0th index,What's in 1st index and up in 2nd index). I'm using c method of DMA in c++ to achieve this. Following is my code- https://code.sololearn.com/cNWxZV9IoG4q/?ref=app
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int main() {
string sentence;
getline(cin,sentence);
stringstream ss(sentence);
string *words;
int count=0;
while(ss>>sentence)
{
if(count==0)
{
words=
(string*)malloc((count+1)*sizeof(string));
*(words+count)=ss.str();
count++;
continue;
}
words=(string*)realloc(words, (count+1)*sizeof(string));
*(words+count)=ss.str();
free(words);
count++;
}
return 0;
}
But , I'm getting error , Could anyone please fix it?