you could be getting into problems if you're constantly changing i, AND changing the length of text3 at the same time. you're using text3 for too many things: a storage of the result, as a length, and a source for the string copy. I would create a separate string to hold the result of the string copy while you do comparisons on text3.
also, the first iteration of your loop changes the first character of the string all the time, you should reverse it to a while() {} loop instead of a do{} loop.