Category프로그래밍/C언어 300제 (90)

중급 90. 문자가 숫자인지 검사하기 (isdigit)

학습 내용 : 문자열에서 숫자 문자를 판별하는 방법을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include #pragma warning(disable:4996) void main() { char *string = "Today 2020.03.04"; char buffer[100] = {0,}; int count = 0; while(*string) { if (isdigit(*string)) { buffer[count] = *string; count++; } string++; } puts(buffer); } 실행 화면 :

중급 89. 문자가 알파벳인지 검사하기 (isalpha)

학습 내용 : 문자열에서 알파벳 문자를 판별하는 방법을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include #include #pragma warning(disable:4996) void main() { char *string = "Today 2020.03.04"; char buffer[100] = {0,}; int count = 0; while(*string) { if (isalpha(*string)) { buffer[count] = *string; count++; } string++; } puts(buffer); } 실행 화면 :

중급 88. 실수를 문자열로 변환하기 3 (gcvt)

학습 내용 : printf() 함수와 유사한 기능을 하는 gcvt() 함수를 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #pragma warning(disable:4996) void main() { double value1 = 3.14e10; double value2 = -3.14e10; char buffer[100]; gcvt(value1, 3, buffer); puts(buffer); gcvt(value2, 3, buffer); puts(buffer); } 실행 화면 :

중급 87. 실수를 문자열로 변환하기 2 (ecvt)

학습 내용 : printf() 함수를 사용하지 않고, 지수가 포함된 실수값을 문자열로 변환하는 기본 원리를 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #pragma warning(disable:4996) void main() { double value1 = 3.14e10; double value2 = -3.14e10; char *pstr; int dec; int sign; pstr = ecvt(value1, 3, &dec, &sign); puts(pstr); printf("소수점의 위치는 %d, 부호는 %d입니다.\n", dec, sign); pstr = ecvt(value2, 3, &dec, &s..

중급 86. 실수를 문자열로 변환하기 1 (fcvt)

학습 내용 : printf() 함수를 사용하지 않고, 실수값을 문자열로 변환하는 기본 원리를 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include #pragma warning(disable:4996) void main() { double value1 = 3.141592; double value2 = -3.141592; char *pstr; int dec; int sign; pstr = fcvt(value1, 6, &dec, &sign); puts(pstr); printf("소수점의 위치는 %d, 부호는 %d입니다.\n", dec, sign); pstr = fcvt(value2, 8, &dec, &sign); ..

중급 85. 정수를 문자열로 변환하기 3 (_ultoa)

학습 내용 : itoa() 함수와 기능이 유사한 _ultoa() 함수의 기능을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include #pragma warning(disable:4996) void main() { unsigned value = 98765; int radix = 16; char string[100]; _ultoa(value, string, radix); puts(string); } 실행 화면 :

중급 84. 정수를 문자열로 변환하기 2 (ltoa)

학습 내용 : itoa() 함수와 기능이 동일한 ltoa() 함수의 기능을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #pragma warning(disable:4996) void main() { int value1 = 312; int value2 = -312; int radix = 2; char string[100]; ltoa(value1, string, radix); puts(string); ltoa(value2, string, radix); puts(string); } 실행 화면 :

중급 83. 정수를 문자열로 변환하기 1 (itoa)

학습 내용 : printf() 함수를 사용하지 않고, 숫자값을 문자열로 변환하는 방법을 학습합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include #pragma warning(disable:4996) void main() { int value1 = 11; int value2 = -11; int radix = 10; char string[100]; itoa(value1, string, radix); puts(string); itoa(value2, string, radix); puts(string); } 실행 화면 :

중급 82. 문자열을 실수로 변환하기 2 (strtod)

학습 내용 : 지수가 포함된 문자열을 실수값으로 변환하는 방법을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 #include #include void main() { char *string = "3.14E-10"; double tempDouble = -1; tempDouble = strtod(string, NULL); printf("문자열 %s를 숫자로 변경하면 %E입니다.\n", string, tempDouble); } 실행 화면 :

중급 81. 문자열을 실수로 변환하기 1 (atof)

학습 내용 : 소수점이 포함된 문자열을 실수값으로 변환하는 방법을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include void main() { char *string1 = "3.141592... 원주율"; char *string2 = "원주율 3.141592..."; float tempFloat1 = -1; float tempFloat2 = -1; puts(string1); puts(string2); tempFloat1 = atof(string1); tempFloat2 = atof(string2); printf("%f\n", tempFloat1); printf("%f\n", tempFloat2); } 실행 화면 :