473,623 Members | 3,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15181


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.cpqc orp.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.hel sinki.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.pow ernet.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("Permuta tions of %s : \n", caIn) ;

Permute( caIn ) ;

char caIn2[] = "ABCD" ;
printf("\n\nPer mutations 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(i Len) ;
char* const pcOut = (char*)malloc(i Len+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
-------------------------------------------------------------------------
shankarATcsDoTi ndinaDoTedu
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
5605
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 = lst for x in permute( lst ): yield head + x
1
623
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
2954
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 something like: Word 2 added : "Jack be nimble" Words 10-11 changed to: "the quick brown fox" : "the brown fast quick fox" Words before word 20 removed: "sat in a corner on"
20
41281
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
3209
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
3
3127
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 number, e.g.
2
2991
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
5872
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 dictionary essentially creates the following array: Key Value
16
3056
by: saki | last post by:
Write a program to print all the permutations of a given string.
13
2232
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): for i in range(len(perm)+1):
0
8162
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8662
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8603
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8463
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5560
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4067
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1468
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.