Finding Anagrams

Finding Anagrams

We can fetch the anagrams of a string using following funtion. We can assign characters in temp variable and generate anagram_arr by comparing temp array and anagram_arr.

<?php

function Find_Anagrams($input)

{

$anagram_arr = array();

$len = strlen($input);

for ($i = 0; $i < $len; $i++)

{

for ($j = 0; $j < $len; $j++)

{

// Assigning input to temp variable and calculating anagram_arr

$temp = $input;

$t = $temp[$j];

$temp[$j] = $temp[$i];

$temp[$i] = $t;

if (!in_array($temp, $anagram_arr))

{

$anagram_arr[] = $temp;

}

}

}

return $anagram_arr;

}

$output= Find_Anagrams(”welcome”);

print_r($output);

?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.