Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Counting Strings

Posted on 2003-03-27
4
Medium Priority
?
202 Views
Last Modified: 2010-05-18
Hey,
First of all I apologize for my English. It is not my first language.
I am new with Delphi.
I want to make a program that counts duplicated strings in a ListBox.
My program has a 2 component a button and a List box. If I push the button it must count how many strings or dabble and show it in a messageBox.
can somebody help me plz?

PS: I have tried with Array and For statement, but I didnt succeed.
0
Comment
Question by:kh420
  • 2
4 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 8216998
If speed isn't a problem:

[Listbox called 'LB', Label called 'Label1']

procedure CountDoubles;
var
  lCount : integer;
  lIndex : integer;
  lPos   : integer;
begin
  lCount := 0;

  for lIndex := LB.Items.Count - 1 downto 1 do
  begin
    lPos := LB.Items.Find( LB.Items[lIndex] );
    if ( lPos >= 0 ) and ( lPos < lIndex ) then
    begin
      Inc( lCount );
    end;
  end;

  Label1.Caption = IntToStr(lCount) + ' duplicates';
end;

   
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 8217003
ooopss... 'Find' should be 'IndexOf'

and

after Label1.Caption

it should be

:=

instead of

=
0
 
LVL 9

Accepted Solution

by:
mocarts earned 150 total points
ID: 8217242
my version :)

procedure TForm1.Button1Click(Sender: TObject);
var
  st: TStringList;
  i, idx: integer;
  sMsg: string;
begin
  st := TStringList.Create;
  try
    for i := 0 to ListBox1.Items.Count -1 do begin
      idx := st.indexOf(ListBox1.Items[i]);
      if idx > -1 then
        st.Objects[idx] := pointer(integer(st.Objects[idx]) + 1)
      else
        st.AddObject(ListBox1.Items[i], pointer(0));
    end;
    for i := 0 to st.Count -1 do
      if integer(st.Objects[i]) > 0 then
      begin
        if sMsg <> '' then sMsg := sMsg + #13;
        sMsg := sMsg + '[' + inttostr(integer(st.Objects[i])+1) +'] ' + st[i];
      end;
    if sMsg <> '' then
      ShowMessage('Lines has duplicates:'+#13 + sMsg);
  finally
    st.Free;
  end;
end;

it's better of course to make this proc to some general by passing TStrings (ListBox.Items or Memo.Lines) to it and dealing with them.
wbr, mo.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8223071
my version :-))

unit checkfor_duplicates_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function checkfor_dub_strings(AStringList : TStrings) : Integer;
var
  sl : TStringList;
begin
  result := 0;  //pre-init
  sl := TStringList.Create;
  try
    sl.Sorted := true;
    sl.Duplicates := dupIgnore;
    sl.Assign(AStringList);
    result := AStringList.Count - sl.Count;
  finally
    sl.Free;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage('There are '+inttostr(checkfor_dub_strings(ListBox1.Items))+' Duplicate Entries');
end;

end.

meikl ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question