0

Dear all,
any one knows how to print string out based on combination using php.

the string a b c d
will print

a
b
c
d
ab
ac
ad
bc
bd
cd
abc
abd
acd
bcd
abcd

Thanks in advance....

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by monica singh
0

This should get you started... It permutes the string, working out each combination. But it only works out combinations with the same amount of characters of the input string.

E.g. The 4 letter string "abcd" returns the following 4 letter combinations:
abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc

<?php

	if (isset($_POST['submit']))
	{
		$input = $_POST['inputing'];
		permutate($input,0,inputlen($input)); // call the function.
	}

	// function to generate and print all N! permutations of $input. (N = inputlen($input)).
	function permutate($input,$i,$n) 
	{
		if ($i == $n)
		{
			echo $input ."<br>";
		}
		else 
		{
			for ($j = $i; $j < $n; $j++) 
			{
				swap($input,$i,$j);
				permutate($input, $i+1, $n);
				swap($input,$i,$j); // backtrack.
			}
		}
	}

	// function to swap the char at pos $i and $j of $input.
	function swap(&$input,$i,$j) 
	{
		$temp = $input[$i];
		$input[$i] = $input[$j];
		$input[$j] = $temp;
	}   
	
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
	<input type="text" name="inputing">
	<input type="submit" name="submit" value="Submit">
</form>
0

Hi,

here is the complete code for... all u have to do is jus change the string and its length as per ur requirement.

<?php

        /* 
           [email]hide@address.com[/email] 
           [url]http://staff.k-designs.com.sg/basica/[/url]
 
         */


	function Permutate($strDataIn, $Length, &$PermutateCount) {
	
		for ($i = 0; $i < strlen($strDataIn); $i++) {
			$PermArray[0][$i] = substr($strDataIn, $i, 1);
			$temp[$i]         = substr($strDataIn, $i, 1);
			$temp2[0][$i]     = substr($strDataIn, $i, 1);  
		}                                 
		
		/* for ($i = 1; $i < strLen($strDataIn); $i++) { */
		for ($i = 1; $i < $Length; $i++) {
			for ($k = 0; $k < strLen($strDataIn); $k++) {
				for ($j = 0; $j < sizeof($temp2[$i - 1]); $j++) {
					$PermArray[$i][($k * sizeof($temp2[$i - 1])) + $j] = $temp[$k] . $temp2[$i - 1][$j];
					$temp2[$i][($k * sizeof($temp2[$i - 1])) + $j]     = $temp[$k] . $temp2[$i - 1][$j];
				}		
			}
		}   
		
		$k = 0;

		/* for ($i = 0; $i < strlen($strDataIn); $i++) { */

		for ($i = 0; $i < $Length; $i++) {
			$k += sizeof($PermArray[$i]);
		}
		
		$PermutateCount = $k;
		
		return $PermArray;		      

/*		for ($i = 0; $i < $Length; $i++) {
			for ($j = 0; $j < sizeof($PermArray[$i]); $j++) {
				print $PermArray[$i][$j] . "<br>";
			}	                                
			print "<br>";
		}      
*/		
	}                  
	
	$StartString = "abc"; 
	$len         = 3;  
	
	$Return = Permutate($StartString, $len, $cnt);
	
	print "Returned <b>$cnt</b> permutations.<br><hr>";
	
	for ($i = 0; $i < $len; $i++) {
		for ($j = 0; $j < sizeof($Return[$i]); $j++) {
			print $Return[$i][$j] . "<br>";
		}	                                
		print "<br>";
	}      
		


?>

Edited by monica singh: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.