CrazyEngineers
  • Urgent!!! Problem in C

    Updated: Oct 26, 2024
    Views: 1.1K
    I need to make all the permutations of a string's characters........how do i do this.....

    tiill now what i was doing was allot a digit to each character.....start from 999999......n times(where n is the no of charcters).......till 0......and then check for digits and print the permutation accordingly....

    e.g.: String to be permuted: ajkl
    then i was assigning:
    1 to a
    2 to j
    3 to k
    4 to l

    and looping from 9999 to 0 i was checking for all the numbers which contained the digits 1,2,3,4 and accordingly printed the digit....
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Saandeep Sreerambatla

    MemberSep 14, 2010

    I didnt get you clearly.

    For 4 numbers you have permutations 10000 ??

    Is it what you are doing?
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberSep 14, 2010

    I get what you are saying. You can do it that way, but it is a really inefficient way of doing it.
    You've to replace characters with numbers, do the permutation, and then switch back from numbers to characters.

    Also, it gets very tedious when the string length grows beyond a limit..

    You can easily do this by using pointers and a couple of for loops. Just search for a permutation program in C, and you'll get an idea.
    Are you sure? This action cannot be undone.
    Cancel
  • Saandeep Sreerambatla

    MemberSep 14, 2010

    Hey SS, I didnt get the question!

    Can you explain me ?
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberSep 14, 2010

    Did you mean to say like this

    say if i the entered string is "abcde" then there must be 120 (=4*3*2*1) different words made by the letters abcde.
    and you want to print out different combination of these characters

    correct me if i am wrong
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberSep 14, 2010

    @Goyal: yes. The question is to print all possible permutations of a string. If it's a 5 character string, then number of permutations will be 5*4*3*2*1 = 120.

    @ES: What I think shashank is doing is this.

    Let's say the string is abc. He'll denote a by 1, b by 2 and c by 3. So, the string is 123 now, and we need the permutations of it.
    What he'll do is, search all numbers from 999 to 0 (actually, it's enough if we search numbers from 999 to 100) and look for numbers that contain all of 1, 2 and 3. He'll probably store these numbers in an array.

    Those numbers will be 123, 132, 213, 231, 312 and 321. Now, these numbers will be in an array. The final step is to replace 1 with a, 2 with b and 3 with c in that array. So, we get all permutations.

    The problem with this is, obviously, with more characters in the string, we need to search more numbers..
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register