[Codewars] Simple Pig Latin

Simple Pig Latin

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
std::string pig_it(std::string str) {
string now = "", res = "";
auto work = [&]() {
if(now.length()) {
if(now.length() > 1) res += now.substr(1);
res.push_back(now[0]);
res += "ay";
now = "";
}
};
for(auto& ch : str) {
if(isalpha(ch)) now.push_back(ch);
else {
work();
res.push_back(ch);
}
}
work();
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/30/PS/Codewars/simple-pig-latin/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.