What r u trying to solve. i am not sure why you have two while loops. Please let us know what is the input to it. What would be the output too. This could help us to get to resolve the problem that you face.As far us now i can just understand that you are replacing a pattern with another one that will have space and length of the pattern. Please let us knwo what you want clearly with a example so that we could help you.