기본 틀 :
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;
}
}
'문제 풀기 > C#' 카테고리의 다른 글
78. 피보나치 (0) | 2025.02.25 |
---|---|
77. 이진 변환 반복하기 (Convert.int32, Convert.ToString) (0) | 2025.02.25 |
75. 최댓값과 최솟값 (0) | 2025.02.24 |
74. 신고 결과 받기 (Distinct, Dictionary Value를 List로 만들기) (0) | 2025.02.24 |
73. 공원 산책 (0) | 2025.02.24 |