Posts [프로그래머스] 이상한 문자 만들기 / C++
Post
Cancel

[프로그래머스] 이상한 문자 만들기 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


공백을 기준으로 짝/홀수 인덱스를 판단하기 때문에 idx 변수를 사용했습니다.

풀이 1은 아스키코드에서 알파벳 대/소문자의 차이가 32임을 이용해 풀었습니다.

풀이 2는 대/소문자를 변환해주는 toupper, tolower 함수를 사용해 풀었습니다.


영문 아스키코드 값

A~Z: 65~90

a~z: 97~122


소스 코드


풀이 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>

using namespace std;

string solution(string s) {
    int idx = 0;
    
    for(int i=0; i<s.size(); i++){
        
        if(s[i] == ' '){
            idx=0;
            continue; 
        }
        
        if(idx%2){
            if(s[i] <= 'Z') s[i]+=32;
        }
        else{
            if(s[i] >= 'a') s[i]-=32;  
        }
        idx++;
    }
    
    return s;
}


풀이 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>

using namespace std;

string solution(string s) {
    int idx = 0;
    
    for(int i=0; i<s.size(); i++){
        
        if(s[i] == ' '){
            idx=0;
            continue; 
        }
       
        if(idx%2){
            s[i] = tolower(s[i]);
        }
        else{
            s[i] = toupper(s[i]); 
        }
        idx++;
    }
    
    return s;
}



This post is licensed under CC BY 4.0 by the author.

[프로그래머스] 시저 암호 / C++

[프로그래머스] 약수의 합 / C++

Comments powered by Disqus.