954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Itterating - "a" through "zzzz"

Hi all. I'm attempting to output all of the possible combination from "a" to "zzzz" and I'm wondering if theres an easy way of doing that. As an example output would be ...

a
b
c
....
z
aa
ab
ac
....
az
ba
bb
...

and so on..

Is there a way to just have a binary or hex value, cast it as an ascii to get your first value, then increment it and cast it again.. and so on?

Any help would be much appreciated.

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

This is how I would do it:

$lower = true; //if you want lowercase letters
$t = 4;
$s = 1;
while( $s <= $t ) {
	$i = ( $lower ? 97 : 65 );
	while( $i <= ( $lower ? 122 : 90 ) ) {
		echo str_repeat( chr( $i ),$s ) . '';
		$i++;
	}
	$s++;
}


That what you are talking about?

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 195
 

This is how I would do it:

$lower = true; //if you want lowercase letters
$t = 4;
$s = 1;
while( $s <= $t ) {
	$i = ( $lower ? 97 : 65 );
	while( $i <= ( $lower ? 122 : 90 ) ) {
		echo str_repeat( chr( $i ),$s ) . '';
		$i++;
	}
	$s++;
}

That what you are talking about?


Well sorta.. It would basically generate all teh combinations of a set containing 'a' through 'z'. i.e. {a,b,c,...,z}. So it owuldnt just generate aa, bb, cc.. it would go through all the possible combinations with a as the first character then b as the second character and so on.. sorry if I was not specific enough in my initial post.

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

This code below will do the trick, but it's going to use a lot of memory to run this.
You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?

<?
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
	for($b = 97; $b <= 122; $b++)
	{
		for($c = 97; $c <= 122; $c++)
		{
			for($d = 97; $d <= 122; $d++)
			{
				echo chr($a).chr($b).chr($c).chr($d)."<br/>";
			}
		}
	}
}
?>
SimonMayer
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 14
Solved Threads: 10
 

In fact, I realise that the above code will not produce your single, double and triple character strings.
There are more elegant ways of doing it, but I am not able to put the time into devising one at the moment. One way would be just to repeat the code like so:

<?
for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
	echo chr($a)."<br/>";
}



for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
	for($b = 97; $b <= 122; $b++)
	{
		echo chr($a).chr($b)."<br/>";
	}
}



for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
	for($b = 97; $b <= 122; $b++)
	{
		for($c = 97; $c <= 122; $c++)
		{
			echo chr($a).chr($b).chr($c)."<br/>";
		}
	}
}


for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
{
	for($b = 97; $b <= 122; $b++)
	{
		for($c = 97; $c <= 122; $c++)
		{
			for($d = 97; $d <= 122; $d++)
			{
				echo chr($a).chr($b).chr($c).chr($d)."<br/>";
			}
		}
	}
}
?>
SimonMayer
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 14
Solved Threads: 10
 
This code below will do the trick, but it's going to use a lot of memory to run this. You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?

Thanks so much. Thats exactly what I was looking for. I am sorta unfamiliar with PHPs type casting syntax. I'm trying to stress test a script i'm preparing and just thought this would be a cool way to do it as well as a way to test myself and learn more.

Thanks again!

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You