An optimized version that uses reverse instead of sort for finding the next permutation A utility function to reverse a string str [ l . . h ]
void reverse ( char str [ ] , int l , int h ) { while ( l < h ) { swap ( & str [ l ] , & str [ h ] ) ; l ++ ; h -- ; } }