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;
}
'C++ Algorithm & Study > C++ & Algorithm Strategies' 카테고리의 다른 글
[Programmers] 카펫 (0) | 2023.01.21 |
---|---|
[Programmers] 구명보트 (0) | 2023.01.17 |
[Programmers] 예상 대진표 (0) | 2023.01.10 |
[Programmers] 점프와 순간 이동 (0) | 2023.01.09 |
[Programmers] 삼총사 (0) | 2023.01.07 |