https://programmers.co.kr/learn/challenges?selected_part_id=12079

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s) {
    int idx = -1;
    for (int i = 0; i < s.length(); i++)
    {
        idx++;
        if (s[i] == ' ')
        {
            idx = -1;
            continue;
        }
        int dec = 0;
        if (idx % 2 == 0)
        {
            if (s[i] >= 'a' && s[i] <= 'z')
            {
                dec = -32;
            }
        }
        else
        {
            if (s[i] >= 'A' && s[i] <= 'Z')
            {
                dec = 32;
            }
        }
        s[i] += dec;
    }
    return s;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

toupper() -> 대문자로 변환

tolower() -> 소문자로 변환

 

다 풀고 나서 다른사람의 풀이를 보니 이런 함수가 있었다.

이 함수를 알았다면 더 짧게 짤 수 있었을 것 같다.

+ Recent posts