We help IT Professionals succeed at work.

like brute force

frangers99
frangers99 asked
on
304 Views
Last Modified: 2010-04-04
This is more of a curiosity question, but i was just wondering how could you make a program that creates words based on the concept of brute force, ie. a, b, ab, aab ,abb etc up to a limit of 10 characters, i understancd ord would probably used (ie. the ascii key chart) but i don't know how to use the loops properly, any ideas.
Comment
Watch Question

Author

Commented:
Adjusted points from 25 to 35

Author

Commented:
Any ideas anybody?
Do you want know how make the loops for a,b,aa,ab,... is it? Could you explain a bit more?
Top Expert 2004

Commented:
Could you explain a bit more?

Commented:
I am not exactly sure of what you are asking, but it seems fun to answer a question that I am not exactly sure of, so here I go.  A little recursive fun.  (Delphi grammer could be messed up, but what the heck.)

{Global stuff}
const
  MaxLength: UINT = 10;
  Alphabets: array or char = ['a'..'b'];
var
  WordsList: TStringList;

{Call this one}
procedure Main;
var
  Length: Integer;
begin
  WordsList := TStringList.Create;

  for Length := 1 to MaxLength do
    AddWords(Length, '');
end;

prcedure AddWords(const Length: Integer; Prefix: String);
var
  i: Integer;
begin
  if Length = 0 then
    WordsList.Add(Prefix)
  else
  for i := Low(Alphabet) to High(Alphabet) do
    AddWords(Length - 1, Prefix + Alphabet(i));
end;

If this is not what you wanted, maybe you could tell us some more.  

SY

PS No, I did not drink during the St.Patrick's Day fiasco!  

Author

Commented:
i'm not rejecting your annswer as so i'm gonna try it soon, but what i want is loop that counts from a..z and when it completes that it adds a character aa..zz and outputs all those words and then aaa..zzz.

I want to do what our counting system does but with letters ie 0..9 and when all the 1 digit numbers are used we go 10..99 (use an extra digit) get my idea. Sorry if it's confusing.

Commented:
You can do it simply by using recursive procedure.
Like this simple code:

program Project1;
const
  MaxChars = 5; // length of your generated string
var
  s: set of char;  // Your predefined set of usable characters
  res: string[MaxChars]; // result of each iteration

  procedure Next(k: integer); // k - number of changing character
  var i: integer;
  begin
    for i:=0 to 255 do
      if chr(i) in s then  // check if character in your set
      begin
        res[k] := chr(i);
        if k < MaxChars then
          Next(k+1)
        else
          Writeln(res);
      end;
  end;
 
var j: integer;
begin
  s:=['A'..'Z',' ','a'..'z'];
  res:='';
  for j:=1 to MaxChars do // Generate empty string
    res:=res+chr(1);      //
   
  Next(1); // run process
end.

You can change Writeln to your own processing procedure.

There is the way to do it without recursive procedures. I don't think it is required here.

Author

Commented:
Adjusted points from 35 to 45

Author

Commented:
Sorry Serega your answer is perfect in Turbo Pascal, but i can't work out how to use this code in Delphi...if you can send me the response with Delphi source code then you got yourself the points, but thanks very much for contributing.

Commented:
Hi! I don't completely understand why you reject that code.
It will perfect work in Delphi if you set in your project options "Generate console application" and put the code into Project1.dpr file.
It will also work in any place of Delphi program if you change Writeln to your processing procedure (I don't think that you need only to write generated strings).

Commented:
Serega's solution seems fine (except for little grammar things like "Integer" is missing in "const MaxChar: Integer = 5;").  What exactly does NOT work in Delphi?  Forgive me if I offend you, but do you need help with generating an application or checking syntax in Delphi?  As you may know, all kinds of people come and ask for questions, and we have to know what level you are on exactly to help you out.  

Also, this might be a good time to ask this question too: does your curiosity allow you to use recursive functions/procedures?   You might want to check that out with your curiosity first.  Then we can try to help you better.  

 

Author

Commented:
Sorry, I am pretty new to Delphi, i compiled the EXACT code in Turbo Pascal,  and it ran perfectly, when i put it into Delphi it seemed to treat the word Next (as i the subroutine) as a keyword.

Author

Commented:
i spose in my ignorance i am asking if i want to make this visual what parts of the code go where.

ie. what goes in a button? do i specify the functions, even if you don't answer this Serenga, you got the points.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Sooooooo, you're writing a password hacker, aren't you!? :-)

-- Comment: ----------------
This may sound trivial, but if you have a function "Next" which Delphi assumes is a keyword (something I cannot imagine, but okay), then just rename the function. To _Next(), FNext(), DoNext() or whatever!

Author

Commented:
Adjusted points from 45 to 50

Author

Commented:
thanks Serenga, your solution was excellent. Sorry for making it harder for you, but i appreciate it very much. I was wondering if you have a webpage, i'd be interested to look at some of your other progs. to learn something from.

thanks anyway.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.