프로그래머스 - 내 풀이/프로그래머스 Lv1

프로그래머스 / 연습문제 / 문자열 다루기 기본

ENUM01 2020. 5. 1. 10:37

https://programmers.co.kr/learn/courses/30/lessons/12918

 

프로그래머스

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

programmers.co.kr

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
26
27
28
29
#include <string>
#include <vector>
 
using namespace std;
 
bool solution(string s) {
    bool answer = true;
    string a = "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (s.length() == 4 || s.length() == 6)
    {
        for (int i = 0; i < s.size(); i++)
        {
            for (int j = 0; j < a.size(); j++)
            {
                if (s[i] == a[j])
                {
                    answer = false;
                    break;
                }
            }
        }
    }
    else
    {
        answer = false;
    }
    
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

나는 !isdigit() 함수를 몰랐기 때문에,

string a 에 알파벳을 다 넣고 검사했다.

 

isdigit()함수는 매개 변수로 char 타입이 10진수 숫자로 변경이 가능하면 true(1) , 아니면 false(0) 를 반환하는 함수이다.

 

isdigt()함수를 사용하면 다음과 같이 간단하게 풀 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <vector>
using namespace std;
 
bool solution(string s) {
    bool answer = true;
 
    for (int i = 0; i < s.size(); i++)
    {
        if (!isdigit(s[i]))
            answer = false;
    }
 
    return s.size() == 4 || s.size() == 6 ? answer : false;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter