473,396 Members | 2,036 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.

function to permute a string

hello all, not sure if it's appropriate to post here. If not, I am sorry
and please direct me to the right groups. Thanks.
I write this function to permute a string. e.g. if the input string is
"abc", I am expecting to see the output: "abc", "acb", "bac", "bca",
"cab", "cba"
there is n! output for a string of size(n). Here is my code fragment,
Can anyone points out how to improve my code so that I get the right
output. You can see the current input for just running the code. I believe
the algorithm is correct. Thanks.
void permute(char *s)
{
int i,j,k;
char str[100];

if (strlen(s) <= 1)
printf("%s\n", s);
else {
for (i=0; i< strlen(s); i++) {
memset(str, 0, sizeof(str));
printf("%c",s[i]);
strcpy(str,s);
for (j=i; j<strlen(s); j++)
str[j] = str[j+1];

str[strlen(s)-1] = 0;
permute(str);
}
}
}
Nov 14 '05 #1
2 4663
On 4 Aug 2004 15:15:50 GMT, Yuan Zhong <yz****@henri.math.fsu.edu>
wrote:
hello all, not sure if it's appropriate to post here. If not, I am sorry
and please direct me to the right groups. Thanks.
I write this function to permute a string. e.g. if the input string is
"abc", I am expecting to see the output: "abc", "acb", "bac", "bca",
"cab", "cba"
there is n! output for a string of size(n). Here is my code fragment,
Can anyone points out how to improve my code so that I get the right
output. You can see the current input for just running the code. I believe
the algorithm is correct. Thanks.
void permute(char *s)


I would implement this by passing two arguments. To start call

permute( "", "abc" );

This recusively makes the following calls:

permute( "a", "bc" );
permute( "b", "ac" );
permute( "c", "ab" );

Each call permute() takes one character from the second string
and appends it to the first string.

permute( "a", "bc" ) will call:
permute( "ab", "c" )
permute( "ac", "b" )

permute( "b", "ac" ) will call:
permute( "ba", "c" )
permute( "bc", "a" )

permute( "c", "ab" ) will call:
permute( "ca", "b" )
permute( "cb", "a" )

Nick.
Nov 14 '05 #2
Yuan Zhong wrote:
.... snip ...
I write this function to permute a string. e.g. if the input
string is "abc", I am expecting to see the output: "abc", "acb",
"bac", "bca", "cab", "cba" there is n! output for a string of
size(n). Here is my code fragment, Can anyone points out how to
improve my code so that I get the right output. ...

.... snip code ...

I didn't attempt to check your code. Here is a version I have
used for some time:

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

/* Public domain, by C.B. Falconer. Attribution appreciated. */

/* Things get out of hand when larger than 8 */
#define MAXWORD 12

/* ------------------ */

/* exchange 0th and ith char in wd */
void trade(char *wd, unsigned int i)
{
char c;

c = *wd;
*wd = wd[i];
wd[i] = c;
} /* trade */

/* ------------------ */

/* Form all n char permutations of the characters in the
string wd of length lgh into outstring at index ix.
Output the results to stdout. */
void jumble(char *wd, unsigned int lgh,
unsigned int ix, /* output place to fill */
unsigned int n, /* max out places to fill */
char *outstring)
{
unsigned int i;

if (0 == n) {
outstring[ix] = '\0';
puts(outstring);
}
else
for (i = 0; i < lgh; i++) {
trade(wd, i); /* nop when (0 == i) */
outstring[ix] = *wd;
jumble(wd+1, lgh-1, ix+1, n-1, outstring);
trade(wd, i); /* restore the wd string */
}
} /* jumble */

/* ------------------ */

int main(int argc, char *argv[])
{
unsigned int n, lgh, min;
double max;
char outstring[MAXWORD];

if (argc < 2) {
fprintf(stderr,
"Usage: jumble <baseword> [lgh]\n"
" where the (optional) lgh specifies the\n"
" maximum length of the output words\n");
return 0;
}
lgh = strlen(argv[1]);
if (lgh >= MAXWORD) argv[1][lgh = MAXWORD-1] = '\0';

min = lgh;
if ((argc > 2) && (1 == sscanf(argv[2], "%u", &n)))
if (n && (n <= lgh)) min = n;

for (n = lgh, max = 1.0; n > (lgh - min); n--)
max = max * n;

fprintf(stderr, "string=\"%s\", max=%.0f, len=%u\n",
argv[1], max, min);

jumble(argv[1], lgh, 0, min, outstring);
return 0;
} /* main */

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
Nov 14 '05 #3

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

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
3
by: Richard A. DeVenezia | last post by:
Can a function write another function that has a specific number of nested loops and then run it? i.e. function maker (N) { // does stuff that creates function doer() // invoke doer doer() }
7
by: Sriram Rajagopalan | last post by:
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
1
by: candexis | last post by:
So I am working with classes and I have the following Method, which rotate a variable: unsigned __int64 Subkeys::RotaionalLeftShift() { unsigned __int64 top_bits = 0; top_bits = K >>...
1
by: Schüle Daniel | last post by:
Hello, I came up with this algorithm to generate all permutations it's not the best one, but it's easy enough # lst = list with objects def permute3(lst): tmp = lenlst = len(lst) def...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
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
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
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.