+ 3

# Have a problem with string permutation code (C)

My String Permutation code doesn't work. If we give n to the length of the given string, the string resets every P(n,2) permutations. For example if you input "solo", after P(4,2)=12 permutations it resets to the permutation "solo" again because of the method I use. (It only works correctly with 3-character strings just because P(3,2)=6 and they have 6 permutations in total) Any Suggestions? https://code.sololearn.com/chPYpwVr8WSx/?ref=app

5 Answers

+ 2

But if I do that it wont be all permutations. As you know n length word has n! permutations but my code can only print P(n,2) of them

+ 1

You forgot to use perm() instead of fact() in your for loop. Tiny mistake. Everything else looks fine.

+ 1

No problem, thanks for replying my question

+ 1

Hello again. I think this task is not possible using a single for loop.
Of all the ways to find string permutations the Heap's algorithm seems to be the closest to your soulution. Take a look:
https://en.wikipedia.org/wiki/Heap%27s_algorithm

0

Oh, I see now. I thought that was the behavior you wanted. I missunderstood your question.
I'm sending you this to tell you that I won't be online for the next 10 hours and I won't be able to take a closer look to your code till then. Sorry. Hopefully someone will join this thread and help you before my return.