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

Function main

Examples/Modules/Chapter 17/Ex17_04A/Ex17_04A.cpp:7–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import <string>;
6
7int main()
8{
9 std::string words[]{ "The", "quick", "brown", "fox", "jumps" };
10 Stack<std::string> wordStack; // A stack of strings
11
12 for (const auto& word : words)
13 wordStack.push(word);
14
15 Stack<std::string> newStack{ wordStack }; // Create a copy of the stack
16
17 // Display the words in reverse order
18 while (!newStack.isEmpty())
19 std::cout << newStack.pop() << ' ';
20 std::cout << std::endl;
21
22 // Reverse wordStack onto newStack
23 while (!wordStack.isEmpty())
24 newStack.push(wordStack.pop());
25
26 // Display the words in original order
27 while (!newStack.isEmpty())
28 std::cout << newStack.pop() << ' ';
29 std::cout << std::endl;
30
31 std::cout << std::endl << "Enter a line of text:" << std::endl;
32 std::string text;
33 std::getline(std::cin, text); // Read a line into the string object
34
35 Stack<const char> characters; // A stack for characters
36
37 for (size_t i{}; i < text.length(); ++i)
38 characters.push(text[i]); // Push the string characters onto the stack
39
40 std::cout << std::endl;
41 while (!characters.isEmpty())
42 std::cout << characters.pop(); // Pop the characters off the stack
43
44 std::cout << std::endl;
45}

Callers

nothing calls this directly

Calls 3

pushMethod · 0.45
isEmptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected