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

Function main

Exercises/NoModules/Chapter 20/Soln20_02/Soln20_02.cpp:14–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12*/
13
14int main()
15{
16 std::string words[]{ "The", "quick", "brown", "fox", "jumps" };
17 std::stack<std::string> wordStack; // A stack of strings
18
19 for (const auto& word : words)
20 wordStack.push(word);
21
22 std::stack<std::string> newStack{ wordStack }; // Create a copy of the stack
23
24 // Display the words in reverse order
25 while (!newStack.empty())
26 {
27 std::cout << newStack.top() << ' ';
28 newStack.pop();
29 }
30
31 std::cout << std::endl;
32
33 // Reverse wordStack onto newStack
34 while (!wordStack.empty())
35 {
36 newStack.push(wordStack.top());
37 wordStack.pop();
38 }
39
40 // Display the words in original order
41 while (!newStack.empty())
42 {
43 std::cout << newStack.top() << ' ';
44 newStack.pop();
45 }
46
47 std::cout << std::endl;
48
49 std::cout << std::endl << "Enter a line of text:" << std::endl;
50 std::string text;
51 std::getline(std::cin, text); // Read a line into the string object
52
53 std::stack<char> characters; // A stack for characters
54
55 for (size_t i{}; i < text.length(); ++i)
56 characters.push(text[i]); // Push the string characters onto the stack
57
58 std::cout << std::endl;
59 while (!characters.empty())
60 {
61 std::cout << characters.top(); // Pop the characters off the stack
62 characters.pop();
63 }
64
65 std::cout << std::endl;
66}

Callers

nothing calls this directly

Calls 3

pushMethod · 0.45
emptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected