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

Function main

Examples/NoModules/Chapter 07/Ex7_05.cpp:5–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include <string>
4
5int main()
6{
7 std::string text; // The string to be searched
8 std::string word; // Substring to be found
9 std::cout << "Enter the string to be searched and press Enter:\n";
10 std::getline(std::cin, text);
11
12 std::cout << "Enter the string to be found and press Enter:\n";
13 std::getline(std::cin, word);
14
15 size_t count{}; // Count of substring occurrences
16 size_t index{}; // String index
17 while ((index = text.find(word, index)) != std::string::npos)
18 {
19 ++count;
20 index += word.length(); // Advance by full word (discards overlapping occurrences)
21 }
22
23 std::cout << "Your text contained " << count << " occurrences of \""
24 << word << "\"." << std::endl;
25}

Callers

nothing calls this directly

Calls 1

findMethod · 0.45

Tested by

no test coverage detected