C# 알고리즘 코드카타
41. 이상한 문자 만들기
잼잼재미
2023. 11. 22. 09:45
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;
}
}