Gidhub BE Developer

Programmers - 시저 암호

2018-07-24
goodGid

Problem

Problem URL : 시저 암호


[1] Answer Code (18. 07. 24)


string solution(string s, int n) {
    string answer = "";
   
    for(int i=0; i<s.length(); i++){
        if (s[i] >= 97 && s[i] <= 122) { // Lower Case
            s[i] = s[i] + n;
            if( s[i] > 122){
                s[i] -= 26;
            }
        }
        else if (s[i] >= 65 && s[i] <= 90) { // Upper Case
            s[i] = s[i] + n;
            if( s[i] > 90){
                s[i] -= 26;
            }
        }
    }
    answer = s ;

    return answer;
}



[2] Answer Code (18. 09. 12)

string solution(string s, int n) {
    int size = (int) s.size();
    for(int i=0; i<size; i++){
        if( s[i] == ' ' ) continue;
        int value = s[i] - '0' + 48;
        if( value >= 97 && value <= 122){ // Lower Case
            if( value + n > 122)
                s[i] = 'a' + value + n - 123;
            else
                s[i] = value + n;
        }
        if( value >= 65 && value <= 90){ // Upper Case
            if( value + n > 90)
                s[i] = 'A' + value + n - 91;
            else
                s[i] = value + n;
        }
    }
    
    return s;
}

Back : ASCII Value

Comments

Content