Earlier I've posted a problem asking Why does this code not work using split and array slices.

Read that and try to solve it yourself before reading this.

Here is my solution: