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

+ Recent posts