# Thread: Help needed with strings permutations program

1. ## Help needed with strings permutations program

Hi all.. this is my first post. I am trying to solve the strings permutation challenge given right here in cprogramming.com, wherein you have to find and print all permutations of a user-entered string. I know the basic concept of permutations by developing factorials of numbers. however i am not able to properly represent the logic in terms of the C syntax. and i dont want to see the solution right away! please help me by giving some guidelines and hints to start developing the code.. thanks..

2. > however i am not able to properly represent the logic in terms of the C syntax.

Post the said logic to provide proof of your effort to solve the problem. Then, turn the basic procedure into pseudocode if doing so will help you code the final program. Once you've done those two things, we will be glad to help you with the C syntax.

3. I didn’t realize we had challenges here.
Found it: Challenges - Permutation Challenge - Cprogramming.com

String Permutation Challenge

Here is another mathematical problem, where the trick is as much to discover the algorithm as it is to write the code: write a program to display all possible permutations of a given input string--if the string contains duplicate characters, you may have multiple repeated results. Input should be of the form

permute string
and output should be a word per line.

Here is a sample for the input cat

cat
cta
act
atc
tac
tca

4. I always like starting with a very simple test case - like the "cat" example, above. Except, I also always like the permutations to all be in strict sorted order - that makes it quite a bit easier to see if the answers are all in place.

The way I would do this by hand, without a computer, would be to use swaps, at the right hand side of the string. Swap once,
act
atc //c and t have been swapped. A is the head and is left alone now

Now move one column to the left:
cat //if the rest of the letters are not in sorted order, I re-sort the letters after the c. Insertion or Bubble sort is quite good at this kind of short sorts

Work it through by hand, as you would do it, until you see the pattern -- that pattern becomes the basis for your logic. When broken down into small steps for the computer, it will become your pseudo code for the algorithm you'll use. The pseudo code can then become the basis for your actual code.

Lots of info on this, on the net. Google is your friend! And Welcome! to the forum!

5. This looks like a job for recursion!

Sorry that I can't provide more help than that since I've never made a program to permute a word like this, but I'd probably do it recursively.

6. OIC - The factorial number system IS the algorithm you wanted to use, but you are unfamiliar with the c-syntax.

You are going to need to get a string from the user
Check the size of the string

To address a character in the string, you can use array notation or pointers. (string_name[0] is the first letter, string_name[2] is the third, string_name[n-1] is the nth letter in the string). And string_name1[0] = string_name2[3] puts the forth character of string_name2 into the first character of string_name1. You can use pointers, but it may be a bit to get your head around.

As for the patterns that you want to implement, there are for/while/do-while loops that are easy to get your head around - Especially if you already know another computer programming language.

7. ok..m trying to do a neat pseudocode..will try posting it asap..