text reverse algorithm