Draw the problem out, with a ladder leaning against the wall, and label the respective lengths. You will notice it forms a right-angle triangle, with the ladder as the hypotenuse.
So the theorem is
a^2+b^2 =c^2
where c represents the ladder length (the hypotenuse), and so a can represent the distance of the ladder from the base of the building (the answer), and b is where the ladder reaches on the building.
Therefore,
a^2+32^2=40^2
Since a is what we need, rearrange the above formula to solve for a and you will get your answer. (Remember the units ft)