Created
May 3, 2020 05:46
-
-
Save DongguemYoo/50a0d34a3ac56771dcf79d19c4de9a3c to your computer and use it in GitHub Desktop.
코딩테스트 연습 시저 암호
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//검색 아스킷 코드표 | |
//string to 아스킷코드 | |
//아스킷코드 to string | |
//Convert ===> using System | |
//내가 한 | |
public string solution(string s, int n) | |
{ | |
string answer = ""; | |
//c# string to 아스킷 코드 | |
//int code = Convert.ToInt32(char); | |
//아스킷코드 to string | |
//char c = (char)code; | |
int code=0; | |
for (int i = 0; i < s.Length; i++) | |
{ | |
code = Convert.ToInt32(s[i]); | |
if (65 <= code && code <= 90) | |
{ | |
//대문자 | |
code += n; | |
if (code> 90) | |
{ | |
code = code % 90+64; | |
} | |
} | |
else if(97<=code && code <=122) | |
{ | |
code += n; | |
//소문자 | |
if (code > 122) | |
{ | |
code = code % 122+96; | |
} | |
} | |
answer += (char)(code); | |
} | |
return answer; | |
} | |
//다른분이 하신 코드 | |
public class Solution { | |
public string solution(string s, int n) { | |
string answer = ""; | |
for(int i=0;i<s.Length;i++) | |
{ | |
answer += sijusiju(s[i] , n); | |
} | |
return answer; | |
} | |
public string sijusiju(char c, int n) | |
{ | |
if(c == ' ') | |
{ | |
return " "; | |
} | |
if(c >= 'a' && c <= 'z') | |
{ | |
if((char)( c+n) > 'z' ) | |
{ | |
c = (char)( c+n - 26);//이부분이 포인트 인듯 z 가 넘어가면 알파뱃수만큼 빼줘서 초기화! | |
} else | |
{ | |
c = (char)( c+n); | |
} | |
} | |
if(c >= 'A' && c <= 'Z') | |
{ | |
if((char)( c+n) > 'Z' ) | |
{ | |
c = (char)( c+n - 26);//이부분이 포인트 인듯 z 가 넘어가면 알파뱃수만큼 빼줘서 초기화! | |
} else | |
{ | |
c = (char)( c+n); | |
} | |
} | |
return c.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment