Skip to content

Instantly share code, notes, and snippets.

@youngjinmo
Last active September 11, 2019 10:36
Show Gist options
  • Save youngjinmo/e75f3aba19df2f686d18e9fb49f7a34f to your computer and use it in GitHub Desktop.
Save youngjinmo/e75f3aba19df2f686d18e9fb49f7a34f to your computer and use it in GitHub Desktop.
문자열을 argument로 받아서 짝수번째 문자는 대문자로, 홀수번째 문자는 소문자로 바꾸어서 다시 문자열로 반환해보았다.
public class quizTest_01 {
public static String changeByIndex(String str){
String[] strArray = str.split("");
for(int i=0; i<strArray.length; i++){
// 인덱스가 0 또는 짝수인 문자를 대문자화
if(i==0 || i%2==0){
strArray[i] = strArray[i].toUpperCase();
} else {
// 인덱스가 홀수인 문자를 소문자화
strArray[i] = strArray[i].toLowerCase();
}
}
// 문자열로 변환
String changed = "";
for(int i=0; i<strArray.length; i++) {
changed += strArray[i];
}
return changed;
}
public static void main(String[] args){
String data = "Hello World";
System.out.println(changeByIndex(data);
// 출력 : hElLo wOrLd
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment