프로그래밍
프로그래머스 C# 시저 암호((char)(), 96, 122)
노마드선샤인
2023. 1. 22. 20:06
728x90
public class Solution {
public string solution(string s, int n) {
string answer = "";
char[] temp = s.ToCharArray();
for(int i = 0; i < s.Length; ++i)
{
if ((temp[i] >= 'a' && temp[i] <= 'z')||(temp[i] >= 'A' && temp[i] <= 'Z'))
{
if (char.IsUpper(temp[i]))
{
temp[i] = (char)((temp[i] + n - 'A') % 26 + 'A');
}
else
{
temp[i] = (char)((temp[i] + n - 'a') % 26 + 'a');
}
}
}
return new string(temp);
}
}
/////
using System;
public class Solution {
public string solution(string s, int n) {
string answer = "";
char[] c = s.ToCharArray();
int temp = 0;
for(int i = 0; i < s.Length; i++)
{
temp = c[i];
if(temp == 32)
{
}
else if(temp >= 65 && temp <= 90 && temp + n > 90 )
{
temp = temp + n - 26;
}
else if(temp >= 97 && temp <= 122 && temp + n > 122 )
{
temp = temp + n - 26;
}
else
{
temp += n;
}
answer += Convert.ToChar(temp).ToString();
}
return answer;
}
}
728x90