GitHubSeob

C++ / 프로그래머스 / JadenCase 문자열 만들기 본문

Programmers/Level 2

C++ / 프로그래머스 / JadenCase 문자열 만들기

GitHubSeob 2023. 6. 22.

문제

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

 

프로그래머스

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

programmers.co.kr

 

문제풀이

소문자를 대문자로 변환해주는 toupper, 대문자를 소문자로 변환해주는 tolower을 사용했다.

s[0]은 s[0]을 대문자로, 나머지 인덱스는 for문을 이용한다.

s[idx - 1]이 공백이고, s[idx]가 공백이 아니면 s[idx]가 첫 문자이므로 toupper을 하여 대문자로,

그 외의 경우는 tolower을 이용해 소문자로 변환한다.

 

코드

#include <string>
#include <vector>
#include <cctype>
using namespace std;

string solution(string s) {
    s[0] = toupper(s[0]);
    for (int idx = 1; idx < s.size(); ++idx) {
        if (s[idx - 1] == ' ' && s[idx] != ' ') {
            s[idx] = toupper(s[idx]);
        }
        else {
            s[idx] = tolower(s[idx]);
        }
    }

    return s;
}