문제 풀기/C#

76. JadenCase 문자열 만들기

kagan-draca 2025. 2. 25. 14:00

기본 틀 :

 

public class Solution {
    public string solution(string s) {
        string answer = "";
        return answer;
    }
}

 

먼저 매개 변수로 주어진 s를 모두 소문자로 변환 시켜줬다.

 

왜냐하면 단어의 첫 글자를 대문자, 이외의 단어는 소문자로 바꾸는 행위가 매우 불편했기 때문이다.

 

그래서, 모든 글자를 소문자로 변환 후 단어의 첫글자를 대문자로 바꿀 수 있도록 만들어줬다.

 

다음으로는 반복문으로 문자열 s를 순회하면서 s[index]가 숫자인지, 공백인지, 소문자인지 구분해줬다.

 

이때 하나의 bool 변수를 이용해 해당 글자가 공백 이후의 첫 글자인 소문자이면 대문자로 변환할 수 있도록 만들어줬다.

using System;
using System.Linq;

public class Solution {
    public string solution(string s) 
    {
        s = s.ToLower();
        string result = "";
        bool emptyCheck = true;
        for(int i = 0; i < s.Length; i++)
        {
            if(char.IsDigit(s[i]))
            {
                result += s[i];
                emptyCheck = false;
            }
            else if(s[i] == ' ' && !emptyCheck)
            {
                result += s[i];
                emptyCheck = true;
            }
            else if(char.IsLower(s[i]) && emptyCheck)
            {
                result += char.ToUpper(s[i]);
                emptyCheck = false;
            }
            else result += s[i];
        }
        
        return result;
    }
}