Algorithm/C++ - 프로그래머스

프로그래머스 - 문자열 다루기 C++

ㅇㅇ잉 2021. 2. 14. 04:35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <vector>
#include <iostream>
 
using namespace std;
 
bool solution(string s) {
    bool answer = true;
    
   for(int i=0;i<s.length();i++){
        if(!((s[i]-'0'>=0&& (s[i]-'0'<=9))){
                answer=false;
        }
    }
    
    if(!(s.length()==4 || s.length()==6)) answer=false;
    
    return answer;
}
cs

 

 

이런 문제 풀이도 있었다.

isdigit함수는 숫자를 판단하는 함수이다.

char type이 10진수 숫자로 변경이 가능하다면 0이 아닌 숫자(=true), 변경이 불가능하다면 0(=false)를 반환한다.

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;
}
cs