PHP anagrammes et permutations

<SCRIPT LANGUAGE=JavaScript>
function OnYva2()
{
var mot=""
mot=prompt("Entrez votre mot ou phrase :",mot)
if(mot==null) {return}
else
{alert("L'anagramme est :"+ inv(mot))}
}

function der(x)
{return x.charAt(x.length-1)}

function corps(x)
{return x.substr(0,x.length-1)}

function inv(x)
{
if (x.length==1)
{return x}
else
{return der(x)+inv(corps(x))}
} </SCRIPT>
<FORM ACTION="" METHOD=POST>
<CENTER>
<HR>
<INPUT TYPE=button NAME=Bouton VALUE="Lancer le programme" onclick="OnYva2()">
<HR>
</CENTER>
</FORM>
<?php
#source:http://www.phpindex.com/trucsetastuces/trucsetastuces_lire.php3?element=148
$chaine1="j";
$chaine2="e";
$chaine3="m";
$chaine4="b";
$chaine5="f";
$chaine6="r";

// Permutation de $chaine1 avec $chaine2

#list($chaine1, $chaine2) = array ($chaine2, $chaine1);
list($chaine1, $chaine2, $chaine3, $chaine4, $chaine5, $chaine6) = array ($chaine3, $chaine1.$chaine2.$chaine3, $chaine1); 
echo $chaine1;
echo $chaine2;
?> 
#! /usr/bin/php
<? 
#################
#pour afficher les permutations d'une chaîne de caractère fournie en ligne de commande
#source: http://www.webmasterworld.com/forum88/778.htm
#adapted fred radeff, radeff.red 25 nov 2008
#################
print "Input:\n";
$name = trim(fgets(STDIN));
#print "hello $name !";
$test="";
for($i=0;$i<strlen($name);$i++){
#echo substr($name,$i,1).";";
$test.=substr($name,$i,1).";";
}
$test=ereg_replace(";$","",$test);
#echo "\ntest input: " .$test;

$yourArr=explode(";",$test);
 
$n=count($yourArr);
 
 
for ($i=0; $i <= $n; $i++) $pArr[$i]=$i; //The permutation array.
 
function PrintPerm()
{
global $yourArr,$pArr,$n;
 
for ($i=1; $i <= $n; $i++) echo $yourArr[$pArr[$i]-1];
#echo "<br>";
echo "\n";
 
return;
}
 
function swapThem($i,$j)
{
global $pArr;
 
$temp = $pArr[$i];
$pArr[$i] = $pArr[$j];
$pArr[$j] = $temp;
}
 
function NextPerm()
{
global $pArr,$n;
 
$k = $n-1;
while ($pArr[$k] > $pArr[$k+1]) $k--;
if ($k == 0) return(0);
else
{
$j = $n;
while ($pArr[$k] > $pArr[$j]) $j--;
swapThem($j,$k);
$r = $n;
$s = $k+1;
while ($r > $s)
{
swapThem($r,$s);
$r--;
$s++;
}
}
PrintPerm();
return(1);
}
 
//Print the array values
PrintPerm();
while (NextPerm()); //Permute and print
?> 
  • info/myphp/anagramme.txt
  • Dernière modification : 2026/03/05 09:14
  • de radeff