C++ Algorithm & Study/C++ & Algorithm Strategies

[Programmers] 이상한 문자 만들기

GameChoi 2023. 1. 16. 20:40

https://school.programmers.co.kr/learn/courses/30/lessons/12930

 

프로그래머스

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

programmers.co.kr

0. Headers

#include <string>
using namespace std;

1. 알고리즘

 - 짝수는 대문자, 홀수는 소문자로 만들어야 하므로 toupper, tolower 함수 사용

   - 공백이 나올경우 index 초기화

string solution(string s) {
    for (auto st : s)
    {
        if (st == ' ') { str.push_back(' '); index = 0; continue; }
        else (index % 2 == 0) ? str.push_back(toupper(st)) : str.push_back(tolower(st));
        index++;
    }
    return str;
}

2. 완성 코드

#include <string>
using namespace std;

string solution(string s) {
    int index = 0;
    string str = "";
    
    for (auto st : s)
    {
        if (st == ' ') { str.push_back(' '); index = 0; continue; }
        else (index % 2 == 0) ? str.push_back(toupper(st)) : str.push_back(tolower(st));
        index++;
    }
    return str;
}