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

Function main

Exercises/Modules/Chapter 17/Soln17_06/Soln17_06.cpp:11–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9#include <cctype>
10
11int 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}

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