473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String Permutation function in C

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
Nov 13 '05 #1
7 15168


Sriram Rajagopalan wrote:
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.

Nov 13 '05 #2
g??d w?y ?? ¶§-ncrypt??n... keep it up!!!

"carl mcguire" <sp***************@spamcop.net> wrote in message
news:3F**************@spamcop.net...


Sriram Rajagopalan wrote:
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.

Nov 13 '05 #3
Greetings.

In article <VY*****************@news.cpqcorp.net>, Sriram Rajagopalan wrote:
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.

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #4
carl mcguire <sp***************@spamcop.net> scribbled the following:
Sriram Rajagopalan wrote:
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:

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".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson
Nov 13 '05 #5
Sriram Rajagopalan wrote:
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).

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6
"Sriram Rajagopalan" <sr****************@hp.com> wrote in message news:<pQ*****************@news.cpqcorp.net>...
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[i] = 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[i] ) continue ;
pcOut[iLevel] = pcIn[i] ;
pcMark[i] = 1 ;
DoPermute( pcIn, pcOut, pcMark, iLen, iLevel+1 ) ;
pcMark[i] = 0 ;
}

return ;

}

-------------------------------------------------------------------------
- shankar
-------------------------------------------------------------------------
shankarATcsDoTindinaDoTedu
Nov 13 '05 #7
nrk
Sriram Rajagopalan wrote:
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[i], str[i] = str[j], str[j] = temp;
permute(str, i+1, len);
temp = str[i], str[i] = 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.
Regards,
Sriram Rajagopalan


Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
1
by: user | last post by:
Hello I have Array of 50 ints. I want to receive random permutation, so in each int will be different number from 0-49. Is there any class for permutation ? Thanx Michal
3
by: Csaba Gabor | last post by:
I'm comparing the text of (snippets of) web pages which I expect to be quite different or quite similar. In the case where they are similar, I would like to display the more recent one and say...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
3
by: weidongtom | last post by:
Hi, I have been working at this problem, and I think I need a permutation algorithm that does the following: Given a list of elements that are either a character or a character follows by a...
2
by: peace357 | last post by:
I am trying to write two recursive functions involving two strings, 1)CAT & 2)MAN. My function needs to print out: TACMAN ATCMAN CTAMAN TCAMAN ACTMAN CATMAN
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
16
by: saki | last post by:
Write a program to print all the permutations of a given string.
13
by: sillyhat | last post by:
Hello, can someone please help. I found the following code at http://code.activestate.com/recipes/252178/ def all_perms(str): if len(str) <=1: yield str else: for perm in all_perms(str):...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.