인풋값 s에 대해서 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하는 문제이다.

C++ 의 isdigit() 함수를 사용하면 쉽게 풀 수 있다.

isdigit 함수를 사용하지 않고, 알파벳 아스키코드 값보다 크거나 작은 지 비교하면서 풀어도 될 것 같다.


헤더 파일 : <cctype>

기본형 : int isdigit(int c);

  • intput c 가 숫자가 아니면 0을 리턴

#include <string>
#include <vector>
#include <cctype>

using namespace std;

bool solution(string s) {
    bool answer = true;

    int s_len = s.length();

    if(s_len == 4 || s_len == 6)
    {
        for(int i = 0; i < s_len; i++)
        {
            if(isdigit(s[i]) == 0)
            {
                answer = false;   
            }
        }
    }
    else
    {
        answer = false;
    }
    return answer;
}

+ Recent posts