Algorithms

Moderators: None (Apply to moderate this forum)
Number of posts: 786

This Forum Only

How to loop through all possible ASCII characters Posted by Zeedolph on 24 Jan 2003 at 9:57 AM
Hey guys.

I am curious as to how I can loop through all possible words, basically.

I can do something like this:
Make an array of characters of A - Z ASCII(65 - 90)
.
.
.
For ii = 65 To 90
j = j + 1
Char(j) = Chr\$(ii)
Next ii
.
.
.
But I can only hard-code for a given length of characters:
.
.
.
For i = 1 to 26
For i2 = 1 to 26
For i3 = 1 to 26
...
strGeneratedWord = Char(i) & Char(i2) & Char(i3)...
...
Next i3
Next i2
Next i
.
.
.
The problem with the above code is that it will only loop for a given
number of characters, and only that amount, but you have to hard-code
an extra for-loop or take one away to increase/decrease.

There must be an easier way.

Let's say I wanted to loop through all possible phone numbers...all
I would have to do is just say i = 0 to 9999 and all numbers between
would be every possible 4-digit number.

So let's say I wanted to see every 1 through 10 character word possible:

A To ZZZZZZZZZZ

(A,B,C....AA,AB,AC...AAA,AAB,AAC...)

This is boggling my mind.

Thanks!

Re: How to loop through all possible ASCII characters Posted by Josh Code on 20 Feb 2003 at 5:46 AM
This message was edited by Josh Code at 2003-2-20 5:49:57

Number base convertion is related to this.
You can think of a word as a number with the base being 26.
Start out with the value being 0 (all 'a' characters in the string).
Create an add procedure to increment (add 1 to) this value. Every time you add 1 to the value you will create a new combination of letters.

digits:
'a' = 0, 'b' = 1, 'c' = 2...'y' = 24, 'z' = 26

ascii values: 'a' = 97, 'x' = 120, 'z' = 122

Here is an example in c++ that will print a bunch of different words to the console:
```#include <stdio.h>
#include <iostream>
// used for the cout routine

#define len1 10
// define the length of your word

#define MaxLetter 'z'
#define MinLetter 'a'
// define the bounds of the digits
char TheWord[len1] = "aaaaaaaaa";

void incValue()
{
/* this can be equated to incrementing a decimal number like 128639999 by looping
from the last digit(farthest right)
If the digit is less than 9, increment and stop looping.
else set digit to 0.

128639999 would become 128640000.
*/
unsigned char exitloop=1;

for (int c=len1-2; (c>0)&&(exitloop!=0);c--) // loop from last digit to first digit of the string
{
if (TheWord[c]<MaxLetter)
{
TheWord[c]++;
exitloop = 0;
//break; // break the for loop because a digit has been incremented
}
else
TheWord[c]=MinLetter;
}
}

void LoopThroughValues()
{
// this will output the first 100 words
for (int x=0;x<110;x++)
{
incValue();
cout << TheWord;
cout << "\n"; // break the line
}
}

int main()
{
LoopThroughValues();
cout << "There are your words.  Now, just type something.";
cin >> TheWord;
return 0;
}
```

I hope that helps.

Re: How to loop through all possible ASCII characters Posted by Zeedolph on 20 Feb 2003 at 6:39 AM
Wow! Thanks a lot for posting that code!

,Zeedolph

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic