MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Exercises/NoModules/Chapter 17/Soln17_06/Soln17_06.cpp:10–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

push_backMethod · 0.45
findMethod · 0.45
element_exists_atMethod · 0.45
front_iteratorMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected