숫자 값으로 구성된 문자열이 주어지면 주어진 숫자를 단어로 바꾸는 작업입니다.
입력 "361"이 있는 것처럼; 그러면 출력은 "삼백육십일"과 같은 단어로 표시되어야 합니다. 다음 문제의 해결을 위해 우리는 1, 10, 100 등의 숫자와 위치를 염두에 두어야 합니다.
코드는 0에서 9999까지의 4자리 숫자만 지원합니다. 따라서 입력은 0에서 9999 사이여야 합니다.
장소가 다음과 같도록 1,111을 고려해 보겠습니다.

예시
Input: “1234” Output: one thousand two hundred thirty four Input: “7777” Output: seven thousand seven hundred seventy seven
주어진 문제를 해결하기 위해 사용할 접근 방식 -
- 입력을 문자열로 받습니다.
- 다른 값에 대한 배열 만들기.
- 길이에 따른 입력의 길이를 확인하여 출력을 보여줄 위치를 결정합니다.
- 장소에 따라 출력이 표시됩니다.
알고리즘
Start
Step 1 → In function convert(char *num)
Declare and initialize int len = strlen(num)
If len == 0 then,
fprintf(stderr, "empty string\n")
Return
End If
If len > 4 then,
fprintf(stderr, "Length more than 4 is not supported\n")
Return
End If
Declare and initialize a char *single_digit[] = { "zero", "one", "two","three", "four","five","six", "seven", "eight", "nine"}
Declare and initialize a char *tens_place[] = {"", "ten", "eleven", "twelve","thirteen", "fourteen","fifteen", "sixteen","seventeen", "eighteen", "nineteen"}
Declare and Initialize a char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"}
Declare and initialize char *tens_power[] = {"hundred", "thousand"}
Print num
If len == 1 then,
Print single_digit[*num - '0']
Return
End If
While *num != '\0
If len >= 3
If *num -'0' != 0
Print single_digit[*num - '0']
Print tens_power[len-3]
End If
Decrement len by 1
End If
Else
If *num == '1' then,
Set sum = *num - '0' + *(num + 1)- '0'
Print tens_place[sum]
Return
End If
Else If *num == '2' && *(num + 1) == '0' then,
Print “twenty”
Return
End else If
Else
Set i = *num - '0'
Print i? tens_multiple[i]: ""
Increment num by 1
If *num != '0' then,
Print single_digit[*num - '0']
End If
End Else
Increment num by 1
End Else
End while
Step 2 → In function main()
Call function convert("9132")
Stop 호출 예시
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//function to print the given number in words
void convert(char *num) {
int len = strlen(num);
// cases
if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported\n");
return;
}
// the first string wont be used.
char *single_digit[] = { "zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"};
// The first string is not used, it is to make
// array indexing simple
char *tens_place[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
// The first two string are not used, they are to make
// array indexing simple
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"};
char *tens_power[] = {"hundred", "thousand"};
// Used for debugging purpose only
printf("\n%s: ", num);
// For single digit number
if (len == 1) {
printf("%s\n", single_digit[*num - '0']);
return;
}
// Iterate while num is not '\0'
while (*num != '\0') {
// Code path for first 2 digits
if (len >= 3) {
if (*num -'0' != 0) {
printf("%s ", single_digit[*num - '0']);
printf("%s ", tens_power[len-3]); // here len can be 3 or 4
}
--len;
}
// Code path for last 2 digits
else {
// Need to explicitly handle 10-19. Sum of the two digits is
//used as index of "tens_place" array of strings
if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
printf("%s\n", tens_place[sum]);
return;
}
// Need to explicitely handle 20
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty\n");
return;
}
// Rest of the two digit numbers i.e., 21 to 99
else {
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digit[*num - '0']);
}
}
++num;
}
}
int main() {
convert("9132");
return 0;
} 출력
nine thousand one hundred thirty two