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

[Programmers] JadenCase 문자열 만들기

GameChoi 2023. 2. 3. 18:50

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

 

프로그래머스

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

programmers.co.kr

0. Headers

#include <string>
using namespace std;

1. 알고리즘

 - 공백이 있을 경우 bool 값을 두고 확인

    - 처음 값은 무조건 대문자로 오기 때문에 to upper 함수 사용

string solution(string s) {
    string answer = "";
    bool start = false;
    
    for (auto str : s)
    {
        if (answer == "") { answer += toupper(s[0]); continue; }        
        if (str == ' ') {start = true; answer += str; continue; }; 
    }
    
    return answer;
}

 - bool 변수가 true가 된다면 대문자로 변경 및 false가 되면 소문자로 변경

string solution(string s) {  
    for (auto str : s)
    {
        if (start)  { start = false; answer += toupper(str);  continue; }
        else answer += tolower(str);
    }
    return answer;
}

2. 완성 코드

#include <string>
using namespace std;

string solution(string s) {
    string answer = "";
    bool start = false;
    
    for (auto str : s)
    {
        if (answer == "") { answer += toupper(s[0]); continue; }        
        if (str == ' ') {start = true; answer += str; continue; }; 
        
        if (start)  { start = false; answer += toupper(str);  continue; }
        else answer += tolower(str);
    }
    
    return answer;
}