반응형
문제).
정수형 데이터를 3자리씩 콤마(,)로 구분하는 문자열로 변환하는 함수를 구현하시오.
실행 예1).
입력)
5000000
결과).
5,000,000
답은 아래에... ↓
스스로 풀어보시고... ↓
아래 답과 비교해보세요. ↓
숫자를 Comma로 구분된 문자열로 변환하는 함수
#include <stdio.h>
#include <string.h>
const char *number2comma(long n)
{
static char comma_str[64];
char str[64];
int idx, len, cidx = 0, mod;
sprintf(str, "%ld", n);
len = strlen(str);
mod = len % 3;
for(idx = 0; idx < len; idx++) {
if(idx != 0 && (idx) % 3 == mod) {
comma_str[cidx++] = ',';
}
comma_str[cidx++] = str[idx];
}
comma_str[cidx] = 0x00;
return comma_str;
}
반응형
'C언어 문제 > 표준함수 구현' 카테고리의 다른 글
strcpy()함수를 포인터와 배열로 구현하기 (5) | 2019.12.21 |
---|---|
strcmp()함수를 포인터와 배열로 구현하기 (0) | 2019.12.21 |
strcat()함수를 포인터와 배열로 구현하기 (0) | 2019.12.20 |
strlen()함수를 포인터와 배열로 각각 구현하기 (0) | 2019.12.20 |