We describe the preparation of atom-number states with strongly interacting bosons in one dimension, or spin-polarized fermions. The procedure is based on a combination of weakening and squeezing of the trapping potential. For the resulting state, the full atom number distribution is obtained. Starting with an unknown number of particles $N_i$, we optimize the sudden change in the trapping potential which leads to the Fock state of $N_f$ particles in the final trap. Non-zero temperature effects as well as different smooth trapping potentials are analyzed. A simple criterion is provided to ensure the robust preparation of the Fock state for physically realistic traps.