-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPigIt.cpp
More file actions
42 lines (37 loc) · 1.09 KB
/
PigIt.cpp
File metadata and controls
42 lines (37 loc) · 1.09 KB
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
34
35
36
37
38
39
40
41
42
//https://www.codewars.com/kata/520b9d2ad5c005041100000f
#include<algorithm>
#include<string>
#include<vector>
#include<cctype>
#include<sstream>
bool isnotAlphanumeric(const std::string& input) {
return std::any_of(input.begin(), input.end(), [](char symb) {
return !std::isalpha(symb) && !std::isdigit(symb);
});
}
std::string pig_it(std::string str) {
std::vector<std::string> words;
char separator = ' ';
size_t pos = 0;
while ((pos = str.find(separator)) != std::string::npos) {
words.push_back(str.substr(0, pos));
str.erase(0, pos + 1/*space*/);
}
words.push_back(str);
std::stringstream result;
for (auto& element : words) {
if ((element.size() == 1) && (isnotAlphanumeric(element))) {
result << element << separator;
} else {
char movedChar = element[0];
element.erase(0, 1);
element.push_back(movedChar);
result << element << "ay" << separator;
}
}
auto ans = result.str();
if (ans.back() == ' ') {
ans.pop_back();
}
return ans;
}