| 9 | #include <cctype> |
| 10 | |
| 11 | int main() |
| 12 | { |
| 13 | std::string text; // Stores input prose or poem |
| 14 | std::cout << "Enter a poem or prose over one or more lines.\n" |
| 15 | << "Terminate the input with #:\n"; |
| 16 | getline(std::cin, text, '#'); |
| 17 | |
| 18 | SparseArray<LinkedList<std::string>> lists; // Sparse array of linked lists |
| 19 | const std::string_view letters {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; |
| 20 | |
| 21 | // Extract words and store in the appropriate list |
| 22 | // A list in the SparseArray is selected by the index in letters of the first letter in a word. |
| 23 | const std::string_view separators {" \n\t,.\"?!;:"}; // Separators between words |
| 24 | size_t start {}; // Start of a word |
| 25 | size_t end {}; // separator position after a word |
| 26 | while (std::string::npos != (start = text.find_first_not_of(separators, start))) |
| 27 | { |
| 28 | end = text.find_first_of(separators, start+1); |
| 29 | const auto word{ text.substr(start, end - start) }; |
| 30 | const auto letter{ static_cast<char>(std::toupper(word[0])) }; |
| 31 | lists[letters.find(letter)].push_back(word); |
| 32 | start = end; |
| 33 | } |
| 34 | |
| 35 | // List the words in order 5 to a line |
| 36 | const size_t perline {5}; |
| 37 | |
| 38 | for (size_t i {}; i < std::size(letters); ++i) |
| 39 | { |
| 40 | if (!lists.element_exists_at(i)) |
| 41 | continue; |
| 42 | |
| 43 | size_t count {}; // Word counter |
| 44 | for (auto iterator { lists[i].front_iterator() }; iterator; iterator.next()) |
| 45 | { |
| 46 | std::cout << iterator.value() << ' '; |
| 47 | if (!(++count % perline)) |
| 48 | std::cout << std::endl; |
| 49 | } |
| 50 | std::cout << std::endl; |
| 51 | } |
| 52 | std::cout << std::endl; |
| 53 | } |
nothing calls this directly
no test coverage detected