글
프로그래머스 C# 가운데 글자(string 몇 번째 문자 가져오기, JAVA)
프로그래밍
2023. 1. 21. 14:05
728x90
SMALL
public class Solution {
public string solution(string s) {
string answer = "";
if(s.Length%2 == 1)
{
answer = s[s.Length/2].ToString(); //홀수면 한 개
}
else
{
answer = s[s.Length/2-1].ToString() + s[s.Length/2].ToString(); // 짝수면 두 개
}
return answer;
}
}
/////
class StringExercise{
String getMiddle(String word){
int length = word.length();
int mid = length / 2;
return length%2==0 ? word.substring(mid-1, mid+1) : word.substring(mid, mid+1) ;
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args){
StringExercise se = new StringExercise();
System.out.println(se.getMiddle("power"));
}
}
728x90
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 두 정수 사이의 합(삼항연산자, JAVA) (0) | 2023.01.21 |
---|---|
프로그래머스 C# 올바른 괄호(시간 최소화, 괄호 알고리즘, JAVA) (0) | 2023.01.21 |
프로그래머스 C# 평행(GetLength, Distinct, float-소수, Count, JAVA) (0) | 2023.01.21 |
프로그래머스 C# 크기가 작은 부분 문자열(long.Parse, JAVA parseLong, substring) (0) | 2023.01.21 |
프로그래머스 C# 연속된 수의 합(Array.Fill, JAVA) (0) | 2023.01.20 |