글
프로그래머스 C# 이상한 문자 만들기(Split, 공백 처리)
using System;
public class Solution {
public string solution(string s) {
string answer = "";
// 입력된 문자열을 공백을 기준으로 잘라서 저장
string[] temp = s.Split(" "); // {try} {hello} {world}
// for문을 돌려서 문자열을 하나씩 호출함.
for (int i = 0; i < temp.Length; i++)
{
// 호출된 문자열에서 차례로 문자 하나씩을 꺼냄
for (int j = 0; j < temp[i].Length; j++)
{
// 짝수는 대문자로, 홀수는 소문자로 바꿈
if (j % 2 == 0)
{
answer += Char.ToUpper(temp[i][j]);
}
else
{
answer += Char.ToLower(temp[i][j]);
}
}
// 띄어쓰기가 전부 생략되어있으므로 문자열이 하나 끝날때마다 띄어쓰기를 넣어줌.
// 단, 마지막에는 띄어쓰기를 추가하면 안되므로 temp.Lenght-1로 설정해준다.
if (i != temp.Length-1)
{
answer += " ";
}
}
return answer;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 제일 작은 수 제거하기(array.where) (0) | 2023.01.23 |
---|---|
프로그래머스 C# 평균 구하기(double) (0) | 2023.01.23 |
프로그래머스 C# 핸드폰 번호 가리기(for문 안에 if문, 길이-4) (0) | 2023.01.23 |
프로그래머스 C# 짝수와 홀수(음의 정수 생각하기, 나누기) (0) | 2023.01.23 |
프로그래머스 C# 정수 내림차순으로 배치하기(ToCharArray, Sort, Reverse, long.Parse) (0) | 2023.01.23 |