PHP ANAGRAM TOOL

the description

This is a program that simply takes a word received through user input, creates anagrams out of that word, checks them against a spellcheck dictionary, then prints only the ones that are valid words. To begin with, this program will only create single-word anagrams of any length that is shorter-than or equal-to the original word.

Before I understood anything about programming, a friend told me that during a job interview he was asked to explain how he would create this program. I was baffled even by the rudimentary explaination he gave me (knowing that I wouldn't understand anything more complex), and ever since I've thought to myself that once I was able to create this program for myself, I'd feel confident that I was a capable programmer.

the implementation

the code

Here is where I show you how I implemented the program. For the anagram tool, I used nested for loops. The nest deepness determines how many characters are being rearranged and analyzed (i.e. the first loop only selects one character to analyze at a time, the second loop takes all two character combinations and analyzes them, etc.), but checks that the nested number is less-than or equal-to the length of the word.

This meathod seems pretty inefficient to me since the nested loop deepness is static, and words only up to the length of the nested deepness I arbitrarily decided on can be anagramized (I stopped at 4 because I think the point of the program has been made at that point. Additional for loops would just be reduntant and wouldn't make me learn any more (and of course learning is the point of this project (just see the tagline below "the sandbox"))).

Ultimately I'd like to try to create a recursive function that "nests" the loops for me. I'm only just now learning about their potential, though, so it may be a while before I get to that point.

<?php
			
$submitted = $_POST['submitted'];
if($submitted == "true")
{
	$pspell_link = pspell_new("en");

	$word = $_POST['word'];
	if(empty($word)) echo "Nice try.  Enter a word for real this time.";
	else {
		echo '<p>Anagrams for the word <span class="highlight">' . $word . '</span>:</p>';
		$len = strlen($word);
		$total_count=0;
		$word_count=0;
		$current_word;
		for($i=0; $i<$len; $i++)
		{
			$current_word = $word[$i];
			if(pspell_check($pspell_link, $current_word))
			{
				echo $current_word . " ";
				$word_count++;
			}
			$total_count++;
			
			for($j=0; $j<$len; $j++)
			{
				if($i!=$j)
				{
					$current_word = $word[$i] . $word[$j];
					if(pspell_check($pspell_link, $current_word))
					{
						echo $current_word . " ";
						$word_count++;
					}
					$total_count++;
					
					for($k=0; $k<$len; $k++)
					{
						if($i!=$j && $i!=$k && $j!=$k)
						{
							$current_word = $word[$i] . $word[$j] . $word[$k];
							if(pspell_check($pspell_link, $current_word))
							{
								echo $current_word . " ";
								$word_count++;
							}
							$total_count++;
							
							for($l=0; $l<$len; $l++)
							{
								if($i!=$j && $i!=$k && $i!=$l && $j!=$k && $j!=$l && $k!=$l)
								{
									$current_word = $word[$i] . $word[$j] . $word[$k] . $word[$l];
									if(pspell_check($pspell_link, $current_word))
									{
										echo $current_word . " ";
										$word_count++;
									}
									$total_count++;
								}
							}
						}
					}
				}
			}
		}
		echo "<br /><br />Total count: " . $total_count . "<br />Word count: " . $word_count;
	}
}
?>