public class Solution {
public string solution(string s) {
string answer = "";
string[] str = s.Split(' ');
int count = 0;
for (int i = 0; i < str.Length; ++i)
{
while (true)
{
int num = str[i].Length;
if (count == num)
{
count = 0;
break;
}
if (count % 2 == 0)
{
answer += str[i][count].ToString().ToUpper();
}
else
{
answer += str[i][count].ToString().ToLower();
}
count++;
}
if (i == str.Length - 1) break;
answer += " ";
}
return answer;
}
}
* 반복문을 두개 사용해서 풀이, 효율성이 떨어짐
* Split() 함수를 사용하지 않고 풀이 가능
* ToUpper(), ToLower() 함수는 string을 대/소문자로 변환 (한글자만 변환할 때는 str[0].Tostring().ToUpper(); 처럼 사용
다른 풀이
public class Solution {
public string solution(string s) {
string answer = "";
int num = 0;
for(int i = 0; i < s.Length; i++)
{
if(num % 2 == 0)
{
answer += s[i].ToString().ToUpper();
}
else
{
answer += s[i].ToString().ToLower();
}
if(s[i] == ' ')
{
num = 0;
}
else
{
num = num + 1;
}
}
return answer;
}
}
'C# 알고리즘 코드카타' 카테고리의 다른 글
43. 크기가 작은 부분문자열 (0) | 2023.11.23 |
---|---|
42. 삼총사 (0) | 2023.11.23 |
40. 3진법 뒤집기 (0) | 2023.11.21 |
39. 최대공약수와 최소공배수 (1) | 2023.11.21 |
38. 직사각형 별찍기 (1) | 2023.11.20 |