대문자와 소문자가 같이 존재하는 문자열을 입력받아 대문자는 소문자로 소문자는 대문자로
변환하여 출력하는 프로그램을 작성하세요.


▣ 입력설명
첫 줄에 문자열이 입력된다. 문자열의 길이는 100을 넘지 않습니다.
문자열은 영어 알파벳으로만 구성되어 있습니다.


▣ 출력설명
첫 줄에 대문자는 소문자로, 소문자는 대문자로 변환된 문자열을 출력합니다.


▣ 입력예제 1

StuDY


▣ 출력예제 1

sTUdy

 

▣ 생각해둘 것

알파벳을 아스키코드값으로 변환했을 때
소문자: 97 ~ 122
대문자: 65 ~ 90

▣ 풀이

public static void main(String[] args) throws IOException {
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

    String str = read.readLine();

    char[] tempStr1 = str.toCharArray();
    char[] tempStr2 = str.toLowerCase().toCharArray();
    String result = "";

    for(int i = 0; i < tempStr1.length; i++) {
        if(tempStr1[i] != tempStr2[i]) {
            result += (char)(tempStr1[i] + 32);
        } else {
            result += (char)(tempStr1[i] - 32);
        }
    }

    System.out.println(result);

}

 

'알고리즘 > 알고리즘' 카테고리의 다른 글

String - 문자 찾기  (0) 2022.10.12
그래프 탐색 방법  (0) 2020.07.30
동적 계획법(다이나믹 프로그래밍)  (0) 2020.07.26

+ Recent posts