Here is the solution I got from my friend: "The loop is not iterating because you break from the loop when elements are not equal. The temporary variable st is outside the loop and not being updated. " So , everything works fine. Thanks to all.