I am also intrigued how anyone else would write this |
Lets see what you do:
FOR EACH digit in [0..9]
IF input CONTAINS digit
THEN disable digit
FOR EACH digit in [0..9]
IF digit IS enabled
THEN PRINT digit |
Do note that if input if 555, then the
IF 555 CONTAINS 5 (which you have on lines 19--24)
will on first iteration change the digit into -1
and on the other two iterations you will compare "-1" with "5".
There is no need to do those two iterations; we already know that input contains at least one 5.
There is statement
break
that helps us to cut the loop short.
There is a shorter path too:
FOR EACH digit in [0..9]
IF input DOES NOT CONTAIN digit
THEN PRINT digit |
Implementation-wise do note that a std::string has an array of characters and a digit is a single character. You treat digits as strings (probably to accomodate the "-1"). You use arrays of strings.
Less is more.
std::string has member function
find()
. You can use it for the CONTAINS test.
Library has
std::find()
, if you want to operate with arrays (or containers).