Implementation of string flipping