Huawei interview question number case conversion
- 2020-04-02 02:05:10
- OfStack
This problem because the limit of 4 digits, so only consider the case of 4 digits, eat share a case conversion source code, there are unlimited number of cases, debugging was very painful, the idea is similar.
void iConvert(int digit)
{
char a[5][10] = {" thousand "," best "," ten ",""," zero "};
char b[11][10] = {" zero "," one "," two "," three "," four "," five "," six "," seven "," eight "," nine "," ten "};
char result[50] = {'0'};
int A[4] = {};
for(int i=3;i>=0;i--)
{
A[i] = digit % 10;
digit = int(digit/10);
}
printf("%d,%d,%d,%dn",A[0],A[1],A[2],A[3]);
int foundZero = 0;
for(int i = 0 ;i<4;i++)
{
if(A[i]>0)
{
strcat(result,b[A[i]]);
strcat(result,a[i]);
}
if(A[i]==0 && foundZero == 0)
{
if(i!=3)//If it is not the last digit, zero is not appended
{
strcat(result,a[4]);
foundZero = 1;
}
}
}
puts(result);
}
Operation results: