Program could be greatly simplified if you know the maximum length of the course.

Otherwise, I advise you to look at this source code - SourceForge.net Repository - [cutest] Contents of...