본문 바로가기

코딩 테스트/프로그래머스

[Level 1, C++] 시저 암호

728x90
반응형

 

시저 암호는 유명한데 교양 보안 관련 수업에서도 나왔었다.

재밌는 암호화 방법인 것 같다.


#include <string>
#include <vector>
#include <iostream>
using namespace std;

string solution(string s, int n) {
    string answer = "";
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == ' ')
        {
            answer.push_back(s[i]);
        }
        else
        {
            for (int u = 0; u < n; u++)
            {
                if (s[i] == 'z')
                {
                    s[i] = 'a';
                    continue;
                }
                else if (s[i] == 'Z')
                {
                    s[i] = 'A';
                    continue;
                }
                else
                {
                    s[i] += 1;
                    continue;
                }
            }
            answer.push_back(s[i]);
        }
        
    }
    cout << answer << endl;
    return answer;
}
728x90
반응형