PEACHCONG

[프로그래머스] 대소문자 바꿔서 출력하기 / 자바(Java) 본문

프로그래머스/코딩 기초 트레이닝

[프로그래머스] 대소문자 바꿔서 출력하기 / 자바(Java)

피치콩 2023. 12. 27. 00:08
🍑note
엄청 간단한 문제인데..
String 클래스의 toLowerCase와 헷갈려서
생각보다 문제 푸는데 시간이 많이 소요되었다🥲

자료형이 String이 아닌 char일 경우
Character.toLowerCase()를 사용해야한다!

기초 중에 기초!! 잊지 말자

 


 

문제
💻 프로그래머스 - 대소문자 바꿔서 출력하기 (Lv.0)

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

풀이
💡 Character.isUpperCase()를 이용하여 대문자 존재 여부 체크
💡 Character.toUpperCase() & Character.toLowerCase()를 이용하여 대/소문자로 변환
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String result = "";
        char ch = ' ';
		
        for(int i = 0; i < a.length(); i++) {
            ch = a.charAt(i);
            if(Character.isUpperCase(ch)) {
                result += Character.toLowerCase(ch);
            } else {
                result += Character.toUpperCase(ch);
            }
        }
        System.out.println(result);

        sc.close();
    }
}

 

 

참고문헌

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

 

String (Java Platform SE 8 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com