Level1 - -
문제
문제 설명
단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.
재한사항- s는 길이가 1 이상, 100이하인 스트링입니다.
s | return |
"abcde" | "c" |
"qwer" | "we" |
풀이
사용언어 : c++
#include <string>
#include <vector>
using namespace std;
string solution(string s) {
string answer;
int index = 0;
if ( s.size() % 2 == 0 ){
index = s.size()/2;
string a,b;
a = s[index-1];
b = s[index];
answer = a + b;
}
else {
index = s.size()/2;
answer = s[index];
}
return answer;
}
// 다른 사람 풀이
#include <string>
#include <vector>
using namespace std;
string solution(string s) {
string answer;
int index = 0;
if ( s.size() % 2 == 0 ){
index = s.size()/2;
string a,b;
a = s[index-1];
b = s[index];
answer = a + b;
}
else {
index = s.size()/2;
answer = s[index];
}
return answer;
}
```
출처 : https://school.programmers.co.kr/learn/courses/30/lessons/12903
'개발 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/C++] 내적 (0) | 2022.11.19 |
---|---|
[프로그래머스/C++] 없는 숫자 더하기 (0) | 2022.11.18 |
[프로그래머스/C++] 음양 더하기 (0) | 2022.11.16 |
[프로그래머스/C++] 나누어 떨어지는 숫자 배열 (0) | 2022.11.15 |
[프로그래머스/C++] 핸드폰 번호 가리기 (0) | 2022.11.14 |