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

Function extractWords

Exercises/Modules/Chapter 20/Soln20_13/Soln20_13.cpp:103–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

101}
102
103Words extractWords(std::string_view text, std::string_view separators)
104{
105 Words words;
106 size_t start{ text.find_first_not_of(separators) }; // Start 1st word
107 size_t end{}; // Index for the end of a word
108
109 while (start != std::string_view::npos)
110 {
111 end = text.find_first_of(separators, start + 1); // Find end separator
112 if (end == std::string_view::npos) // End of text?
113 end = text.length(); // Yes, so set to last+1
114 words.push_back(text.substr(start, end - start));
115 start = text.find_first_not_of(separators, end + 1); // Find next word
116 }
117
118 return words;
119}
120
121WordCounts countWords(const Words& words)
122{

Callers 1

mainFunction · 0.70

Calls 1

push_backMethod · 0.45

Tested by

no test coverage detected