알고리즘/codefights
52>longestWord
Diademata
2018. 2. 4. 13:29
반응형
Define a word as a sequence of consecutive English letters. Find the longest word from the given string. Example For text = "Ready, steady, go!", the output should be longestWord(text) = "steady". |
code>>
std::string longestWord(std::string text) {
std::regex reg("\\w+([a-zA-Z])");
std::smatch m;
std::string str="";
std::string temp = text;
while (std::regex_search(temp, m, reg))
{
for (auto t : m)
{
if (str.length() < t.length())
str = t;
}
temp = m.suffix();
}
return str;
}
반응형