[프로그래머스] Lv1 - 문자열 다루기 기본

까말·2020년 9월 21일
0

문제 설명

문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 a234이면 False를 리턴하고 1234라면 True를 리턴하면 됩니다.

제한 조건

s는 길이 1 이상, 길이 8 이하인 문자열입니다.

입출력 예

 s     return
a234    false
1234    true

풀이

#include <string>
#include <vector>

using namespace std;

bool solution(string s) {
    bool answer = true;
    
    int length = s.length();
    
    if(length != 4 && length != 6)
    {
        return false;
    }
    
    for(int i = 0; i < length; i++)
    {
        char c = s.at(i);
        if(c < '0' || c > '9' )
        {
            return false;
        }
    }
        
    return answer;
}

string의 특정 위치 문자 받기(charAt)
string base = "hello world!";
base.at(0); // 'h'
base.at(1); // 'e'

profile
취업준비중........!!

0개의 댓글