Skip to content

Instantly share code, notes, and snippets.

@youngjinmo
Last active September 11, 2019 10:26
Show Gist options
  • Save youngjinmo/2930ada03134eb309b533e7378730031 to your computer and use it in GitHub Desktop.
Save youngjinmo/2930ada03134eb309b533e7378730031 to your computer and use it in GitHub Desktop.
문자열을 받아서 대문자는 소문자로, 소문자는 대문자로 바꾸는 메서드를 작성했다.
import java.util.Arrays;
public class quizTest_02 {
public static String changeByLetters(String str) {
String[] arr = str.split("");
for (int i = 0; i <arr.length; i++) {
if(arr[i]==arr[i].toLowerCase()){
// 문자가 소문자일 경우, 대문자로 변환
arr[i] = arr[i].toUpperCase();
} else if(arr[i]==arr[i].toUpperCase()){
// 문자가 대문자일 경우, 소문자로 변환
arr[i]=arr[i].toLowerCase();
}
}
// 문자열로 변환
String changed = "";
for(int i=0; i<arr.length; i++){
changed += arr[i];
}
return changed;
}
public static void main(String[] args){
String data = "Hello World";
System.out.println(changeByLetters(data));
// 출력 : hELLO wORLD
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment