String Permutation function in C

S

Sriram Rajagopalan

Hi,
Can ne 1 give me the logic of permuting a string in C language.

What I meant is that suppose if u have a string like

"man"
the output should b:

man
mna
amn
anm
nam
nma

Regards,
Sriram Rajagopalan
 
C

carl mcguire

Sriram said:
Hi,
Can ne 1 give me the logic of permuting a string in C language.

What I meant is that suppose if u have a string like

"man"
the output should b:

man
mna
amn
anm
nam
nma

Regards,
Sriram Rajagopalan
Prbly nt, u cld 4sk in cmp.prgrmmr tho, sum1 thr mght hlp u wth an algrthm.
If u hv a prblm wth the actl C cde u rite, cme bk and sum1 shld b able 2
hlp u.
If u r hving prblems undrstnding ths msg, nxt tme sp3ak fr1kk1ng English
and not d00dsp3ak.
 
S

Sriram Rajagopalan

g??d w?y ?? ¶§-ncrypt??n... keep it up!!!

carl mcguire said:
Prbly nt, u cld 4sk in cmp.prgrmmr tho, sum1 thr mght hlp u wth an algrthm.
If u hv a prblm wth the actl C cde u rite, cme bk and sum1 shld b able 2
hlp u.
If u r hving prblems undrstnding ths msg, nxt tme sp3ak fr1kk1ng English
and not d00dsp3ak.
 
T

Tristan Miller

Greetings.

g??d w?y ?? ¶§-ncrypt??n... keep it up!!!

Your news client is broken. Before posting a message with high-bit
characters, please find one that can set the character set properly.
 
J

Joona I Palaste

carl mcguire said:
Prbly nt, u cld 4sk in cmp.prgrmmr tho, sum1 thr mght hlp u wth an algrthm.
If u hv a prblm wth the actl C cde u rite, cme bk and sum1 shld b able 2
hlp u.
If u r hving prblems undrstnding ths msg, nxt tme sp3ak fr1kk1ng English
and not d00dsp3ak.

U da man, carl! We should probably have some kind of rule: "Questions
written in h4x0r d00dsp33k rather than normal English will not be
answered".
 
R

Richard Heathfield

Sriram said:
Hi,
Can ne 1 give me the logic of permuting a string in C language.

Since ne 1 doesn't seem to be responding, I'll have a go.
What I meant is that suppose if u

"you", not "u"
have a string like

"man"
the output should b:

man
mna
amn
anm
nam
nma

In other words, you take each letter of the string in turn, and set it as
the first letter in the output, and then you recurse into the rest of the
string.

Take a look at http://users.powernet.co.uk/eton/c/ttt.c which demonstrates
the technique (using a noughts-and-crosses board rather than a string, but
the principle is the same).
 
S

Shankar Swamy

Sriram Rajagopalan said:
Hi,
Can ne 1 give me the logic of permuting a string in C language.

What I meant is that suppose if u have a string like

"man"
the output should b:

man
mna
amn
anm
nam
nma

Regards,
Sriram Rajagopalan


You have to do a "mark and recurse" algorithm. That is, taking your
example, first create an array - pcOut[] - that is 3+1 characters
wide. Now place the first character in pcOut[0], and mark that
character. Do this part iteratively with each character in your input.
Once you have a character placed in the pcOut[0] position, you can
place any character but the pcOut[0] character at pcOut[1]. Do a
recursive call by properly passing the marked characters into the
call. That should solve the problem.

There might be other ways of doing it - I did not think hard enough.
I just whipped up what came to my mind when I saw this. This seems to
work though. If you find a better way or find bugs in this, let's
know.

Here is what seems to work - I vefied it on two strings.

PS: Commenting the code is left as an exercise for you, before turning
in the home work :):)

------------------------------------------------------------------------------------


/* gcc -D__TESTING__ -o permutations permutations.c */

#include <stdlib.h>
#include<stdio.h>



void DoPermute( const char* const pcIn, char* const pcOut,
char* const pcMark, const int iLen, const int iLevel ) ;
void Permute( const char* const s ) ;

#if __TESTING__
int main(int argc, char* argv[]) {

char caIn[] = "man" ;
printf("Permutations of %s : \n", caIn) ;

Permute( caIn ) ;

char caIn2[] = "ABCD" ;
printf("\n\nPermutations of %s : \n", caIn2) ;
Permute( caIn2 ) ;

return EXIT_SUCCESS ;

} /* int main(...) */
#endif

void Permute( const char* const s ){

int iLen = strlen(s), i ;
char* const pcMark = (char*)malloc(iLen) ;
char* const pcOut = (char*)malloc(iLen+1) ;
if( !pcMark || !pcOut ){
printf( "Malloc for pcMark or pcOut (or both) failed; bailing
out!\n" ) ;
return ;
}

pcOut[iLen] = 0 ;
for(i=0; i<iLen; i++) pcMark = 0
;

DoPermute( s, pcOut, pcMark, iLen, 0 ) ;

free( pcMark ) ;
free( pcOut ) ;

}


void DoPermute( const char* const pcIn, char* const pcOut,
char* const pcMark, const int iLen, const int iLevel ) {
int i = 0 ; static int iTot = 1 ;


/* If DoPermute(...) from different instances, reset the counter at
the beginning of each instance*/
if(!iLevel) iTot = 1 ;


if( iLen == iLevel ){
printf( "%*d %s\n", 3, iTot++, pcOut ) ;
return ;
}

for(i=0; i < iLen; i++){
if( pcMark ) continue ;
pcOut[iLevel] = pcIn ;
pcMark = 1 ;
DoPermute( pcIn, pcOut, pcMark, iLen, iLevel+1 ) ;
pcMark = 0 ;
}

return ;

}
 
N

nrk

Sriram said:
Hi,
Can ne 1 give me the logic of permuting a string in C language.

What I meant is that suppose if u have a string like

"man"
the output should b:

man
mna
amn
anm
nam
nma
Not the most efficient way of doing it, but hey, who's complaining about
killing the cat by suffocating it in cream :)

#include <stdio.h>

void permute(char *str, const int strt, const int len) {
int i = strt;
int j;
char temp;

for ( i = strt; i < len-1; ++i ) {
for ( j = i+1; j < len; ++j ) {
temp = str, str = str[j], str[j] = temp;
permute(str, i+1, len);
temp = str, str = str[j], str[j] = temp;
}
}
printf("%s\n", str);
}

int main(void) {
char str[] = "1234";

permute(str, 0, sizeof str - 1);

return 0;
}

HTH,
-nrk.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top