updated reverse string algorithm