Algorithm & Code Sharing ⋅ Writings ⋅ Arts ⋅ About |
Text Permutation Text Permutation Generator using recursion technique in PHP Sample input text: ABC Output: ABC, ACB, BAC, BCA, CAB, CBA Source Code: <?php // Text Permutation // Faruq @ 2012 function createPermutationArray($str){ $tmp = array(); if(strlen($str) == 1){ $tmp[] = $str; } for($i = 0; $i < strlen($str); $i++){ $char = substr($str, $i, 1); $rchar = removeChar($str, $i); $p = createPermutationArray($rchar); for($j = 0; $j < count($p); $j++){ $tmp[] = $char . $p[$j]; } } return $tmp; } function removeChar($str, $index){ $tmp = ""; for($i = 0; $i < strlen($str); $i++){ if($i != $index) $tmp .= substr($str, $i, 1); } return $tmp; } echo("Masukkan kata: "); $text = trim(fgets(STDIN)); echo "\n"; $result = createPermutationArray($text); print_r($result); ?> Lihat semua daftar ACS - Download: permutation.php - Tanggal: 3 April 2013 - Kategori: PHP |
© 2025 Muhammad Faruq Nuruddinsyah. All rights reserved. |