-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtag_generator.cpp
More file actions
33 lines (30 loc) · 811 Bytes
/
hashtag_generator.cpp
File metadata and controls
33 lines (30 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
5 kyu
The Hashtag Generator
https://www.codewars.com/kata/52449b062fb80683ec000024
*/
#include <cctype>
#include <optional>
#include <sstream>
#include <string>
using str_t = std::string;
using opt_str_t = std::optional<str_t>;
opt_str_t generate_hashtag(const str_t& str) {
std::istringstream iss(str);
str_t result;
result.reserve(str.size() + 1);
for (str_t token; iss >> token;) {
token[0] =
static_cast<char>(std::toupper(static_cast<unsigned char>(token[0])));
for (size_t i = 1; i < token.size(); ++i) {
token[i] =
static_cast<char>(std::tolower(static_cast<unsigned char>(token[i])));
}
if (result.empty())
result.push_back('#');
result += token;
}
if (result.empty() || result.size() > 140)
return std::nullopt;
return result;
}