https://www.cplusplus.com/reference/cctype/isdigit/?kw=isdigit
isdigit - C++ Reference
function isdigit Check if character is decimal digit Checks whether c is a decimal digit character. Decimal digits are any of: 0 1 2 3 4 5 6 7 8 9 For a detailed chart on what the different ctype functions return for each character of the standard
www.cplusplus.com
isdigit 함수는 입력된 parameter가 숫자인지 구분하는 함수이다.
다음과 같은 형식을 취한다.
int isdigit (int c);
이 때, parameter로 들어가는 건 ASCII 번호로, char로 변환했을 때, 숫자일 경우 1을 반환하고 아닌 경우 0을 반환한다.
사실상 bool 함수에 parameter로 char 변수가 들어가는 셈.
직접 함수를 짜볼 수 있다. 매우 간단하다.
int my_isdigit(int ch)
{
if (ch >= 48 && ch <= 57) // ASCII 코드에서 48 ~ 57은 0 ~ 9 를 나타낸다.
return 1;
return 0;
}
반복문과 isdigit을 이용하면 문자열에서 숫자만 추출 해낼 수 있다.
#include <stdio.h>
#include <ctype.h>
int main ()
{
char *str = "2021년 11월 06일";
char buff[20] = {0,};
int i = 0;
while(*str)
{
if(isdigit(*str))
{
buff[i++] = *str;
}
str++;
}
printf("%s\n",buff);
}
/* 결과
20211106
*/
[C언어] 문자열에서 한글만 추출하기 (0) | 2021.11.08 |
---|---|
[C언어] 문자열에서 영어만 추출하기 (0) | 2021.11.07 |
C와 C++ (0) | 2020.10.01 |
visual studio code C++ 실행 시, code=3221225785가 뜨며 실행이 안될 때 (0) | 2020.09.25 |
[C언어] fprintf, fscanf 에 대해 알아보기 (0) | 2020.07.04 |