Write a function that takes in a c++ string of pig latin. This function should first calculate how many “words” are in the sentence (words being substrings separated by whitespace). It should then allocate an array of the structure Word of this size (so large enough to hold the sentence). It should then store each of the words in that sentence to the array of structures in the piglatin field. The function should then return this array to the calling function with a return statement, along with a size through a reference parameter.
note: This function should also remove all capitalization and special characters except for the very end period, exclamation mark or question mark.
My question: I have completed the first part in which I needed to calculate how many words are in the sentence. I am just confused about how to go about allocating an array of the structure Word to hold the sentence, whilst storing it in the piglatin field.
I am just confused about how to go about allocating an array of the structure Word to hold the sentence, whilst storing it in the piglatin field.
One way to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Word* read_input (std::string input_text, int &size)
{
// Get number of words and set it as return value
size = countWords (input_text);
// allocate array of words
Word *Words = new Word[size];
// fill the array with the words from input_text
for (int i = 0; i < size; i++)
{
Words[i].piglatin = "piglatin"; // words from the input_text
}
return Words;
}