[프로그래머스] 대문자와 소문자

당당·2023년 4월 20일
0

프로그래머스

목록 보기
10/245

https://school.programmers.co.kr/learn/courses/30/lessons/120893

📔문제

문자열 my_string이 매개변수로 주어질 때, 대문자는 소문자로 소문자는 대문자로 변환한 문자열을 return하도록 solution 함수를 완성해주세요.


🚫제한사항

1 ≤ my_string의 길이 ≤ 1,000
my_string은 영어 대문자와 소문자로만 구성되어 있습니다.


📝입출력 예

cipherresult
"cccCCC""CCCccc"
"abCdEfghIJ""ABcDeFGHij"

📝입출력 예 설명

입출력 예 #1

소문자는 대문자로 대문자는 소문자로 바꾼 "CCCccc"를 return합니다.

입출력 예 #2

소문자는 대문자로 대문자는 소문자로 바꾼 "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
profile
MySQL DBA 신입 지원

0개의 댓글