Search code examples
delphiconsole-applicationpassword-checker

How can one verify whether consecutive letters have been entered from the qwerty keyboard in a delphi console application?


Basically, I have an input and I want to verify whether the user input has used 3 or more consecutive letters from the qwerty keyboard layout. By this I mean Q-W-E or Y-U-I-O-P. First I stored the user input in a string variable and used the ansiLowerCase function to convert the input to lowercase. I messed around with declaring the qwerty layout as a constant string and using the strscan function but to no avail. Any help would be greatly appreciated, thanks.


Solution

  • Try something like this:

    function HasThreeConsecutiveLetters(const Str: string): Boolean;
    const
      QwertyLetters: array[0..2] of string = (
        'QWERTYUIOP',
        'ASDFGHJKL',
        'ZXCVBNM'
      );
    var
      I, J, K: Integer;
      S: String;
    begin
      Result := False;
      S := AnsiUpperCase(Str);
      for I := 1 to Length(S) do
      begin
        for J := Low(QwertyLetters) to High(QwertyLetters) do
        begin
          K := Pos(S[I], QwertyLetters[J]);
          if (K <> 0) and
             ((K+2) <= Length(QwertyLetters[J])) and
             (Copy(S, I, 3) = Copy(QwertyLetters[J], K, 3)) then
          begin
            Result := True;
            Exit;
          end;
        end;
      end;
    end;
    

    Then you can do this:

    var
      input: string;
    begin
      input := ...;
      if HasThreeConsecutiveLetters(input) then
        ...
      else
        ...
    end;