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

Coding Planet

2. ๋Œ€๋ฌธ์ž/์†Œ๋ฌธ์ž ์ „ํ™˜, ๋Œ€๋ฌธ์ž/์†Œ๋ฌธ์ž ์ฐพ๊ธฐ - Character.toUpperCase(), Character.isUpperCase() ๋ณธ๋ฌธ

๐ŸŽ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์•Œ๊ณ ๋ฆฌ์ฆ˜

2. ๋Œ€๋ฌธ์ž/์†Œ๋ฌธ์ž ์ „ํ™˜, ๋Œ€๋ฌธ์ž/์†Œ๋ฌธ์ž ์ฐพ๊ธฐ - Character.toUpperCase(), Character.isUpperCase()

jhj.sharon 2023. 5. 3. 20:32
๋ฐ˜์‘ํ˜•

1. ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ

  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์€ ์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ํ˜ผ์šฉ๋˜์–ด ์žˆ๋‹ค. 
  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์˜ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ, ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜๋œ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋ผ

 

 

 

2. ๋ฌธ์ œํ’€์ด(1) - Character.toUpperCase(), Character.isUpperCase()

  • ๋จผ์ € ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด(String)์„ ๋ฌธ์ž ๊ฐ๊ฐ(Char)๋กœ ๋”ฐ์ ธ ์ฃผ๊ธฐ ์œ„ํ•ด toCharArray()๋กœ String(๋ฌธ์ž์—ด)์„ charํ˜• ๋ฐฐ์—ด๋กœ ๋ฐ”๊พผ๋‹ค.
  • ๋ฌธ์ž ๋ฐฐ์—ด์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž์ธ๊ฒฝ์šฐ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋Œ€๋ฌธ์ž์ธ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Character.isLowerCase(๋ฌธ์ž)๋ฅผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์“ด๋‹ค.
  • ๋งŒ์•ฝ ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž(LowerCase)๋ผ๋ฉด true๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ํ•ด๋‹น ๋ฌธ์ž๋ฅผ Character.toUpperCase(๋ฌธ์ž)๋กœ ๋ฐ”๊ฟ”์„œ ์„ ์–ธ๋œ String๋ฌธ์— ์Œ“์•„์ค€๋‹ค -> answer +=Character.toUpperCase(x);

 

2. ๋ฌธ์ œํ’€์ด(2) - ASCII CODE

  • ASCII ์ฝ”๋“œ๋กœ ์•ŒํŒŒ๋ฉง ๋Œ€๋ฌธ์ž๋Š” 65~90๊นŒ์ง€์ด๋‹ค. ์†Œ๋ฌธ์ž๋Š” 97~122์ด๋‹ค.
  • ๋™์ผํ•œ ๋Œ€์†Œ๋ฌธ์ž๋Š” ASCII ์ฝ”๋“œ๋กœ 32 ์ฐจ์ด๊ฐ€ ๋‚œ๋‹ค. ์ฆ‰, ์†Œ๋ฌธ์ž์—์„œ 32๋ฅผ ๋นผ์ฃผ๋ฉด ๋Œ€๋ฌธ์ž ASCII ์ฝ”๋“œ์ด๋‹ค.
  • ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

 

 

3. ๋‚ด ํ’€์ด

 

package algorithm01;

import java.util.Scanner;

public class Class02ChangeChar {

	public String solution(String str) {
		String answer ="";
		
		//str์„ ๋ฌธ์ž์˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
		for(char x : str.toCharArray()) {
			if(Character.isLowerCase(x)) answer +=Character.toUpperCase(x);
			else answer+=Character.toLowerCase(x);
				
			
		}
		
		return answer;
	}
	
	public static void main(String[] args) {
		Class02ChangeChar T = new Class02ChangeChar();
		
		Scanner kb = new Scanner(System.in);
		String str = kb.next();
		System.out.println(T.solution(str));
	}

	
}

๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ์ „ํ™˜๋˜์–ด ์ถœ๋ ฅ๋œ ๋ชจ์Šต

๋ฐ˜์‘ํ˜•
Comments