문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다.
s | return |
---|---|
"a234" | false |
"1234" | true |
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
bool solution(const char* s) {
bool answer = true;
// 문자열 길이 판별 4혹은 6의 문자열 길이
// strlen 문자열 길이, strcopy 문자열 복사, strcat 문자열 결합, strcmp 문자열 비교
if(strlen(s) == 4 || strlen(s) == 6){
// 문자열이기 때문에 반복문 숫자로만 되어 있는지 판별
for(int i=0; i<strlen(s); i++){
if(s[i]<48 || s[i]>57){
answer = false;
break;
}
else{
answer = true;
}
}
}
else{
answer = false;
}
return answer;
}