[자바 코딩테스트] 프로그래머스 12951. JadenCase 문자열 만들기

jeenie's avatar
Sep 09, 2024
[자바 코딩테스트] 프로그래머스 12951. JadenCase 문자열 만들기

문제 설명

JadenCase란 모든 단어의 첫 문자가 대문자이고, 그 외의 알파벳은 소문자인 문자열입니다. 단, 첫 문자가 알파벳이 아닐 때에는 이어지는 알파벳은 소문자로 쓰면 됩니다. (첫 번째 입출력 예 참고)
문자열 s가 주어졌을 때, s를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요.

제한 조건

  • s는 길이 1 이상 200 이하인 문자열입니다.

  • s는 알파벳과 숫자, 공백문자(" ")로 이루어져 있습니다.

    • 숫자는 단어의 첫 문자로만 나옵니다.

    • 숫자로만 이루어진 단어는 없습니다.

    • 공백문자가 연속해서 나올 수 있습니다.

입출력 예

s

return

"3people unFollowed me"

"3people Unfollowed Me"

"for the last week"

"For The Last Week"

코드

// import java.lang.String;

class Solution {
    public String solution(String s) {
        
        String answer = "";
        
        for(int i=0; i<s.length(); i++){
            //첫 번째 문자 or 띄어쓰기 다음 문자 대문자로
            if(i==0 || s.charAt(i-1) == ' ') {
                answer += String.valueOf(s.charAt(i)).toUpperCase(); 
            }
            //나머지 문자 소문자로
            else{
                answer += String.valueOf(s.charAt(i)).toLowerCase();
            }
        }
        
        return answer;
    }
}

알게된 것

Oracle 공식 문서 java.lang.String 를 참고하여 풀었다. String 함수를 많이 까먹어서 parameter, return을 하나하나 찾아보느라 시간이 오래 걸렸다..😶‍🌫️

사용한 함수는 다음과 같다

method

parameter

rerturn

description

length()

x

int (문자열의 길이)

Returns the length of this string.

valueOf(c)

거의 모든 유형이 올 수 있음

String

Returns the string representation of the argument. (String이 아닌 유형을 String으로 변환하고 싶을 때 사용)

charAt(i)

int (인덱스 번호)

char

Returns the char value at the specified index. (String 특정 위치의 문자를 char로 변환)

toUpperCase()

toLowerCase()

x

String

Converts all of the characters in this String to upper case using the rules of the default locale.

마무리하며

  • 다음부턴 문제 풀이 시간을 기록하자

  • String 함수에 얼른 익숙해지자

  • SQL 문제도 풀어보자

Share article
RSSPowered by inblog