Solved

counting substrings in a master-string

Posted on 1999-01-08
11
263 Views
Last Modified: 2010-04-06
Hi experts,

I need a routine that can search within a string.

This routine must count all occurrencies of a1,a2.....h7,h8 (=8x8=64 possibilities)
within a master-string.

For example: If the string to be examined is:
 
master:='Ka1, Lb4,Td4, Kf6, Bh7';
 
the routine I am searching for should return the value 5.

(a1,b4,d4,f6,h7 = 5 occurrencies within the masterstring)


Have you an idea how this could be done in Delphi 3.0 ?

With kind regards

Mathes

 
0
Comment
Question by:mathes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1355287
hi mathes

try this

function CountContents(Source, Delimeter : String) : Integer;
var
  s : String;
  i : Integer;
begin
  i := 0;
  s := source;
  while pos(Delimeter,s) > 0 do
  begin
    delete(s,pos(Delimeter,s),Length(delimeter));
    inc(i);
  end;
  result := i + 1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  edit3.text := inttostr(Countcontents(edit1.text,edit2.text));
end;

meikl
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 30 total points
ID: 1355288
Hi mathes,

This is a solution :

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var i : char;
    j : integer;
    HandyStr : String;
    Count : integer;
begin
 Count := 0;
 HandyStr := Edit1.Text;
 for i := 'a' to 'h' do begin
  for j := 1 to 8 do begin
   while Pos(i+IntToStr(j), HandyStr) <> 0 do begin
    Inc(Count);
    Delete(HandyStr, Pos(i+IntToStr(j),HandyStr), Length(i+IntToStr(j)));
    memo1.Lines.Add(handystr);
   end;
  end;
 end;
 showmessage('sum : '+InttoStr(Count));
end;

end.

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1355289
This is probably even better, ... you can use several ways :

procedure TForm1.Button1Click(Sender: TObject);
var i : char;
    j : integer;
    HandyStr : String;
    Count : integer;
begin
 Count := 0;
 HandyStr := Edit1.Text;
 for i := 'a' to 'h' do begin
  if Pos(i, HandyStr) <> 0 then begin
   for j := 1 to 8 do begin
     while Pos(i+IntToStr(j), HandyStr) <> 0 do begin
      Inc(Count);
      Delete(HandyStr, Pos(i+IntToStr(j),HandyStr), Length(i+IntToStr(j)));
      memo1.Lines.Add(handystr);
     end;
    end;
  end;
 end;
 showmessage('sum : '+InttoStr(Count));
end;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Madshi
ID: 1355290
Hi Mathes,

meikl's solution just count the strings that are splitted by a specific character. Perhaps this is good enough. I'm not sure, what you really need.
Is this string (it's for a chess program, right?) always in this format "Ka1, Lb2" or can there be more than one substring between two "," characters like this "Ka1 Lb2, Tc3"?

Regards, Madshi.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1355291
oops, meikl, didn't saw your comment. Sorry. I didn't refreshed the thread before answering it... Terrible sorry.
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355292
How about this one:

function FindAllOccurances(TheString: String): integer;
type
  TAlphaSet = set of char;
  TNumbSet = set of char;
var
  I: integer;
  TempString: String;
  QueString: String[2];
  TheAlphaSet: TAlphaSet;
  TheNumbSet: TNumbSet;
begin
  QueString := '';
  Result := 0;
  TheAlphaSet := ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                                  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
  TheNumbSet := ['1', '2', '3', '4', '5', '6', '7', '8'];
  TempString := LowerCase(TheString);
  for I := length(TempString) downto 1 do
        begin
      QueString := TempString[I] + QueString;
      if (QueString[1] in TheAlphaSet) and (QueString[2] in TheNumbSet) then
        inc(Result);
    end;
end;

Heath
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355293
Oops, copied wrong version.  This was my final version


function FindAllOccurances(TheString: String): integer;
type
  TAlphaSet = set of char;
  TNumbSet = set of char;
var
  I: integer;
  QueString: String[2];
  TheAlphaSet: TAlphaSet;
  TheNumbSet: TNumbSet;
begin
  QueString := '';
  Result := 0;
  TheAlphaSet := ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                                  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
  TheNumbSet := ['1', '2', '3', '4', '5', '6', '7', '8'];
  for I := length(TheString) downto 1 do
        begin
      QueString := TheString[I] + QueString;
      if (QueString[1] in TheAlphaSet) and (QueString[2] in TheNumbSet) then
        inc(Result);
    end;
end;


Heath
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355294
Mine is cuter.......... :) lol
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1355295
Trimmed some more - stupid error in logic

Oops, copied wrong version.  This was my final version


function FindAllOccurances(TheString: String): integer;
type
  TCharSet = set of char;
var
  I: integer;
  QueString: String[2];
  TheAlphaSet, TheNumbSet: TCharSet;
begin
  QueString := '';
  Result := 0;
  TheAlphaSet := ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
  TheNumbSet := ['1', '2', '3', '4', '5', '6', '7', '8'];
  for I := length(TheString) downto 1 do
   begin
      QueString := TheString[I] + QueString;
      if (QueString[1] in TheAlphaSet) and (QueString[2] in TheNumbSet) then
        inc(Result);
    end;
end;


Heath
0
 

Author Comment

by:mathes
ID: 1355296
Hi experts,

thank you all for your valuable ideas. Madshi is right to assume that my program has to do with chess.
I am currently writing a graphical user interface to a originally command line driven (arrrgghh, very
uncomfortable to handle...) chess program which can't play chess but solve chess problems. The normal
way to delimit the pieces are tabs or blanks: "Ka3 Qd4 Rb7". In some case sometimes the coordinates even
don't have a delimiter!!! The external chess program even understands an input like: Pa3d4f5, which is
identical with: Pa3 Pd4 Pf5. So in my case it is indeed the best solution to count the coordinates and
disregard the  characters preceeding the coordinates of the squares and identifying the concrete piece.
However I know that this method has a severe disadvantage: if there is a substring like "Xa1"
in the masterstring, the piececounter is incremented, too, although there is no piece which
has the specifier "X". But you can't have all in life. Unfortunately the chess program can
handle even very exotic pieces from Chinese chess and fairy chess, so it is not sufficient to
scan for (k,q,r,b,k,p) king,queen,rook,bishop,knight and pawn. But this is now a completely
different story...

With kind regards

Mathes
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1355297
no prob, zif

meikl


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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