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

Function main

Exercises/NoModules/Chapter 07/Soln7_09B.cpp:7–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include <cctype>
6
7int main()
8{
9 std::string text; // The text to be checked
10 std::cout << "Enter some text terminated by *:\n";
11 std::getline(std::cin, text, '*');
12
13 const auto whitespace{ " \t\n\r\f\v" }; // Can be solved using std::isspace() as well...
14
15 const size_t first_letter_index{ text.find_first_not_of(whitespace) };
16 if (first_letter_index == std::string::npos)
17 {
18 // Is an empty string a tautogram? Let's not go there.
19 return 0;
20 }
21
22 const char start_letter{ static_cast<char>(std::toupper(text[first_letter_index])) };
23 bool tautogram{ true };
24
25 for (size_t start_current_word{ first_letter_index };;) // Use an indefinite loop (see the break; statements)
26 {
27 const size_t next_space_index{ text.find_first_of(whitespace, start_current_word) };
28 if (next_space_index == std::string::npos)
29 {
30 break;
31 }
32
33 const size_t next_letter_index{ text.find_first_not_of(whitespace, next_space_index) };
34 if (next_letter_index == std::string::npos)
35 {
36 break;
37 }
38
39 if (std::toupper(text[next_letter_index]) != start_letter)
40 {
41 tautogram = false;
42 break;
43 }
44
45 start_current_word = next_letter_index;
46 }
47
48 std::cout << "The text that you entered is " << (tautogram ? "" : "not ") << "a tautogram.\n";
49 if (tautogram)
50 {
51 std::cout << "All words start with the letter " << start_letter << ".\n";
52
53 std::string text_without_start_letter;
54 for (char c : text)
55 {
56 if (std::toupper(c) != start_letter)
57 {
58 text_without_start_letter.push_back(c);
59 }
60 }
61
62 std::cout << "After removing this letter, the text becomes as follows:\n" << text_without_start_letter << std::endl;
63 }
64}

Callers

nothing calls this directly

Calls 1

push_backMethod · 0.45

Tested by

no test coverage detected