https://school.programmers.co.kr/learn/courses/30/lessons/120893
문자열 my_string
이 매개변수로 주어질 때, 대문자는 소문자로 소문자는 대문자로 변환한 문자열을 return하도록 solution 함수를 완성해주세요.
1 ≤ my_string의 길이 ≤ 1,000
my_string은 영어 대문자와 소문자로만 구성되어 있습니다.
cipher | result |
---|---|
"cccCCC" | "CCCccc" |
"abCdEfghIJ" | "ABcDeFGHij" |
소문자는 대문자로 대문자는 소문자로 바꾼 "CCCccc"를 return합니다.
소문자는 대문자로 대문자는 소문자로 바꾼 "ABcDeFGHij"를 return합니다.
class Solution {
public String solution(String my_string) {
String answer = "";
int temp=0;
for(int i=0;i<my_string.length();i++){
char ch=my_string.charAt(i);
if('a'<= ch && ch<='z'){ //소문자면
temp=ch-'0'-32;
temp=temp+'0';
answer=answer+(char)temp;
}else{
temp=ch-'0'+32;
temp=temp+'0';
answer=answer+(char)temp;
}
}
return answer;
}
}
사실은 .toUpperCase()
라던지 사용했으면 됐을 것 같다.
만약, 소문자이면, ch-'0' (char to int) -32 (a는 97, A는 65)을 해주고
temp=temp+'0'와 (char)temp를 통해 int to char을 해주고
대문자라면 +32