Algorithm/Algorithms Practice
[BOJ/C++] ROT13(11655번)
Patrick_
2022. 10. 18. 14:40
1. 개요
https://www.acmicpc.net/problem/11655
11655번: ROT13
첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.
www.acmicpc.net
1-1. 설명
문자열이 주어졌을 때, ROT13으로 암호화한 다음 출력하는 프로그램을 작성한다.
1-2. 제한 사항
- 첫줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어짐
2. 구현
2-1. 풀이
문자 하나씩 + 13을 해주면 된다. 문자가 알파벳 z를 넘어버리면 - 13을 해주면 된다.
2-2. 코드
#include <iostream>
#include <string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string str, result = "";
getline(cin, str);
for (char ch : str) {
if (ch >= '0' && ch <= '9') {
result += ch;
continue;
}
if (ch >= 'a' && ch <= 'z') {
if (ch + 13 > 'z') ch -= 13;
else ch += 13;
result += ch;
}
else if (ch >= 'A' && ch <= 'Z') {
if (ch + 13 > 'Z') ch -= 13;
else ch += 13;
result += ch;
}
else result += ch;
}
cout << result;
return 0;
}