Thread: Help needed with strings permutations program

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    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. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > 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. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    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. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    ok..m trying to do a neat pseudocode..will try posting it asap..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. skeletal 'dynamic array of strings' function - tweak needed
    By fundamental in forum C Programming
    Replies: 2
    Last Post: 04-18-2012, 02:15 PM
  2. permutations
    By computation in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 02:46 PM
  3. permutations
    By packer in forum C Programming
    Replies: 13
    Last Post: 06-02-2005, 12:54 AM
  4. help needed - filtering a data file (with strings?)
    By Galligan in forum C Programming
    Replies: 1
    Last Post: 11-18-2002, 10:20 AM
  5. Permutations
    By kiddy in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 09:53 AM