By “dynamically” I assume you mean dynamically-allocated
char*
strings? And not std::string and std::vector?
There are several basic possibilities for structure here (and multiple ways to accomplish one of them), and your post is not sufficiently specific to distinguish them. I suspect that what is wanted, though, is explained here:
(
How do I declare a 2d array in C++ using new?|
https://stackoverflow.com/a/936702/2706707)
The trick, then, is to read the individual words and put them in the array.
Again, there are multiple ways to accomplish this. I suggest a two-pass approach:
(1) Count the number of words. Allocate the pointer array.
(2) Read each word and allocate the sub-arrays (strings).
This presumes an additional 1D array used to obtain the entire string (all words at once) from the user before collecting it into individual pieces.
If you
must read one word at a time, you can assume a smaller input array for reading from the user (50 characters should suffice for Basic Latin-1 text), then dynamically resize your pointer array each time you get a new word.
Both of these methods are really inefficient, and I would not use them if requirements did not specify it. Yours appear to, sadly.
Hope this helps.