[Codewars] ROT13

ROT13

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
using namespace std;
std::string rot13(const std::string& str) {
string res = "";
auto work = [](int x) {
return x < 13 ? x + 13 : x - 13;
};
for(auto ch : str) {
if(islower(ch)) res.push_back(work(ch-'a') + 'a');
else if(isalpha(ch)) res.push_back(work(ch-'A') + 'A');
else res.push_back(ch);
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/29/PS/Codewars/rot13/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.