알고리즘/프로그래머스 2단계

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

게임만드는학생 2024. 7. 29. 13:34

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

 

프로그래머스

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

programmers.co.kr

 

#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

string solution(string s) {
    string answer = "";
    
    bool first=true;
    
    for(int i=0;i<s.length();i++)
    {
        if(first&&s[i]!=' ')
        {
            if(isalpha(s[i])&&islower(s[i]))
            {
                s[i]-=32;
            }
            first=false;
        }
        else
        {
            if(s[i]==' ')
                first=true;
            else if(isalpha(s[i]))
                s[i]=tolower(s[i]);
        }
    }
    return s;
}

 

jaden case 문자열은 

문자열에서 각 단어의 첫 글자가 알파벳이면 대문자이며 나머지는 소문자인 문자열이다. 

 

for문을 돌며 첫글자인지 bool값으로 체크하며 그게 알파벳일때 대문자로 바꿔주고

첫글자가 아닐때 소문자로 바꿔줬다.