본문 바로가기

프로그래머스/lv0

문자열 정렬하기(2)

/* 문자열 정렬하기(2)
 * 영어 대소문자로 이루어진 문자열 my_string이 매개변수로 주어질 때,
 * my_string을 모두 소문자로 바꾸고
 * 알파벳 순서대로 정렬한 문자열을 return 하도록 solution 함수를 완성해보세요.
 *
 * my_string    result
 * "Bcad"       "abcd"
 * "heLLo"      "ehllo"
 * "Python"     "hnopty"
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class programmer_0_8 {
    static String a1 = "Bcad";
    static String a2 = "heLLo";
    static String a3 = "Python";
 
    public String solution(String my_String){
        /* 다른사람 풀이
        char[] c = my_string.toLowerCase().toCharArray();
        Arrays.sort(c);
        return new String(c);
        */
        String answer = "";
        my_String = my_String.toLowerCase();
        String[] ar = new String[my_String.length()];
        
        for(int i=0; i<my_String.length(); i++){
            ar[i] = my_String.substring(i,i+1);
        }
        Arrays.sort(ar);
        for(int i=0; i<ar.length; i++){
            answer += ar[i];
        }
 
        return answer;
    }
    public static void main(String args[]){
        programmer_0_8 t = new programmer_0_8();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a3));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

잘라서 배열로 저장하기  (0) 2022.12.06
7의 개수  (0) 2022.12.06
세균 증식  (0) 2022.12.06
제곱수 판별  (0) 2022.12.06
문자열안에 문자열  (0) 2022.12.06