This I believe is one of the first exercises given to students in the first data structure course. The data structure you need to use is Stack. It is a very simple program and code in C++ should not exceed 30 lines.