๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

[JAVA] ๋Œ€์†Œ๋ฌธ์ž ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•˜๊ธฐ ๋ณธ๋ฌธ

Algorithm๐Ÿ’Œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[JAVA] ๋Œ€์†Œ๋ฌธ์ž ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•˜๊ธฐ

bompeach 2023. 4. 22. 22:55

๋ฌธ์ œ ์„ค๋ช… :

์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด str์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ :

๐Ÿ”Ž 1 ≤ str ์˜ ๊ธธ์ด ≤ 10

๐Ÿ”Ž str์€ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ :

์ž…๋ ฅ #1

aBcDeFg

์ถœ๋ ฅ #1

AbCdEfG

 
 
 
๋ฌธ์ œ

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
    }
}

 
 
๋ฐฉ๋ฒ• 1

import java.util.Scanner;

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

 
 
๋ฐฉ๋ฒ• 2

import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String answer = "";

        for(Character c : a.toCharArray()){
            if(Character.isUpperCase(c)){
				answer += Character.toLowerCase(c);
            }
            else if(Character.isLowerCase(c)){
                answer += Character.toUpperCase(c);
            }
        } 
        System.out.println(answer);
    }
}

 
 
๋ฐฉ๋ฒ• 3

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String result = "";

        for(char x : str.toCharArray()) {
            if(Character.isLowerCase(x)) {
                result += Character.toUpperCase(x);
            } else {
                result += Character.toLowerCase(x);
            }

        }
        System.out.println(result);
        }
    }

 

charAt( ) ์ด๋ž€?

Scanner๋กœ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์„ ๋•Œ char ํƒ€์ž…์œผ๋กœ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์—†๊ณ  String ํƒ€์ž…์œผ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์ด ๋‘๊ฐ€์ง€ ์กด์žฌํ•œ๋‹ค. ๊ทธ๋ž˜์„œ charAt( ) ๊ฐ€ ๋“ฑ์žฅํ–ˆ๋Š”๋ฐ, ์–˜๋Š” String์œผ๋กœ ์ €์žฅ๋œ ๋ฌธ์ž์—ด ์ค‘์—์„œ ํ•œ ๊ธ€์ž๋งŒ ์„ ํƒํ•ด์„œ charํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ์• ๋‹ค. ๋•๋ถ„์— Scanner๋ฅผ ์“ฐ๋ฉด์„œ๋„ charํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ฆ‰, String ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ(๋ฌธ์ž์—ด)์—์„œ ํŠน์ • ๋ฌธ์ž๋ฅผ char ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ.

 

String test = "abc";

char target = test.charAt(0);

์œ„์ฒ˜๋Ÿผ String ๋ณ€์ˆ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, 

charAt(i)

i ์ž๋ฆฌ์—๋Š” intํ˜• ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ์›ํ•˜๋Š” ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.