문자열 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'