Not considering language specificities (given the theme of Algoritmos
):
Your solution seems appropriate. You can take user input as a string
and work with each character. Please be careful to make the calculations with the digits equivalent to those characters.
For example, if the user types 12345
, we would have A = 1
, not A = '1'
( A
is the 1
, not the '1'
character that has decimal integer equal to 49
, see ASCII Table ).
Another possible solution would be to read the user input, character the character, in a loop. This solution is quite similar to the previous one. The biggest difference would be the fact that the input is taken character by character, not the complete string.
A third output would be to take the integer value entered by the user and use integer divisions to find the characters of each position.
In this way, for a 12345
entry and considering that:
- the
//
operator gives us the result of the entire division;
- the
%
operator gives us the result of the rest of the entire division.
We would have:
Entrada <- 12345
X <- Entrada
A <- X // 10000
X <- X % 10000
B <- X // 1000
X <- X % 1000
C <- X // 100
X <- X % 100
D <- X // 10
X <- X % 10
E <- X
There are probably other ways to do this, but these are some I could think of.
I believe that the simplest (allying the issue of implementation and clarity of code to others) is the first, also suggested by you.