[C_C++]코딩테스트 연습/[프로그래머스] level 1

[C++] 프로그래머스 - 이상한 문자 만들기

코딩굼벵이 2021. 7. 30. 21:35
728x90

 

내 풀이

길이만큼 포문 돌려서

카운트 세며 짝수면 대문자 홀수면 소문자 띄어쓰기 만나면 초기화

 

딱 이렇게 적어놓고 했더니 금방 풀었다!

예상보다 배점이 높아서 올려본다.

#include <string>
#include <vector>

using namespace std;

string solution(string s) {
    int cnt = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != ' ') {
            if (cnt % 2 == 0) s[i] = toupper(s[i]);
            else s[i] = tolower(s[i]);
            cnt++;
        }
        else cnt = 0;
    }
    return s;
}