How does a program that takes the OPs desired output, and turns in into the OPs input even remotely answer the question the OP is asking? Granted, it's not clear what part of the problem the OP is struggling with, but it is clear what the end result should be.
And that end result is complete different from your solution.