프로그래머스 레벨 1 테스트

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
bool Sort(int a, int b)
{
    return a > b; //내림차순 정렬
//return a < b; //오름차순 정렬
}    
 
string solution(string s) 
{
    sort(s.begin(), s.end(), Sort);
    
    return s;
}
 

 

문자열을 아스키 코드로 변환하면 대문자 A는 '65' 소문자 a는 '97'이기 때문에 내림차순 정렬만해주면 된다.

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <vector>
 
using namespace std;
 
bool solution(string s) 
{    
    
    if(s.size() == 4 || s.size() == 6)
    {
        for(int i = 0 ; i < s.size() ; i++)
        {
            if(isalpha(s[i]))
                return false;
        }
        return true;
    }
 
    return false;
}
 

isdigit() : 문자가 숫자인지 판별해주는 레거시 매크로

 -> 인자를 int 형으로 받는데 이는 아스키 코드값으로 비교하기 때문

 -> isdigit(3)과 isdigit('3')의 결과는 틀리다. '3' = 83으로 변환되어 체크함. 뒤의 사용법이 맞는사용법

 

isalpha() : 문자가 문자(알파뱃)인지 판별해주는 레거시 매크로

 

// 유용한 스트링 관련 매크로

 

isupper() : 문자가 대문자인지 확인

islower() : 문자가 소문자인지 확인

 

toupper() : 소문자로 들어온 문자를 대문자로 변환하여 내보냄. 다른 문자가 들어오면 들어온대로 리턴

tolower() : 대문자로 들어온 문자를 소문자로 변환하여 내보냄. 다른 문자로 들어오면 들어온대로 리턴

 

isalnum() : 숫자 또는 문자(알파벳)인가 확인해준다.

isxdigit() : 16진수인가?

isspace() : 공백 문자인가?

isascii() : 아스키 코드인가?

toascii() : 문자를 아스키코드로 변환

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>
using namespace std;
 
bool solution(string s)
{
    int p = 0;
    int y = 0;
 
    for(int i = 0 ; i < s.length() ; i++)
    {
        string strTmp;
        strTmp = s[i];
        if(strTmp.compare("p"== 0 || strTmp.compare("P"== 0)
            p++;
        else if(strTmp.compare("y"== 0 || strTmp.compare("Y"== 0)
            y++;
    }
    
    return (p == y) ? true : false;
}
 

 

+ Recent posts