Mutual conversion between strings and Integers
1. The integer is converted to a string. You can add '0' and then sort it in reverse order.
Adding '0' will implicitly convert the number to the char type.
# Include
# Include
Int main (void)
{
Int num = 12345, j = 0, I = 0;
Char temp [7], str [7];
While (num)
{
Temp [I] = num % 10 + '0 ';
I ++;
Num = num/10;
}
Temp [I] = 0''
Printf (temp = % s, temp );
I = I-1;
// The converted string is in reverse order and must be reversed.
While (I> = 0)
{
Str [j] = temp [I];
J ++;
I --;
}
Str [j] = 0''
Return 0;
}
2. If the string is converted to an integer, You can subtract '0' and multiply it by 10. If the string is reduced to '0', It is implicitly converted to the number of int type.
# Include
# Include
Int main (void)
{
Int num = 12345, j = 0, I = 0, sum = 0;
Char temp [7] = {'1', '2', '3', '4', '5', ''}, str [7];
While (temp [I])
{
Sum = sum * 10 + (temp [I]-'0 ');
I ++;
}
Printf (sum = % d, sum );
Return 0;
}