SRL Archive - Research & Development Lounge

Count Function


smithsps

2008-04-26 21:13:17

scar, even though there is probably one.

It just counts how much a string appears in a string.

Function CountStr(StrToCount, Str: string): integer;
begin
repeat
if (Pos(StrToCount, Str) <> 0) then
begin
Delete(Str, Pos(StrToCount, Str), Length(StrToCount));
Inc(Result);
end;
until(Pos(StrToCount, Str) = 0);
end;
Please report if it doesn't work.

Thanks To:
Santa_Clause
&
mixster
For Helping me improve my script.

'Pos' definition:
function pos(substr, s: string): LongInt;
//Returns position of substring in string. Returns 0 if not found.

smithsps

2008-04-26 21:49:50

Examples:

Count('a', 'aa');
Would return 2 because there is 2 'a's in the string.

writeln(IntToStr(Count('a', 'aba')));
Would write '2'.

Count('aa', 'aa');
Would return 1 because there is 1 'aa' in 'aa'.

Count('aa', 'aba');
Would return 0 because there is no 'aa' in 'aba'.

MylesMadness

2008-04-26 22:01:37

Oh I thought your function did something different so I posted the scar function.

Da 0wner

2008-04-27 00:08:52

Why would you do this?

Santa_Clause

2008-04-27 04:18:19

Function Count(Letter, Word : String) : Integer;
Var
I : Integer;
Begin
For I := 1 To Length(Word) Do
If Letter = Word[I] Then
Inc(Result);
End;

Faster :p

mixster

2008-04-27 06:19:47

You may want to change it so it can count the occurrences of a string. It's easy enough, just change it to:

Function Count(Letter, Mess: string): integer;
begin
repeat
if (Pos(Letter, Mess) <> 0) then
begin
Delete(Mess, Pos(Letter, Mess), Length(letter)); // Changed 1 to Length(letter);
Result := Result + 1;
end;
until(Pos(Letter, Mess) = 0);
end;
And maybe change the name to CountStr so it doesn't get confused with a mathematical function.

ShowerThoughts

2008-04-27 06:23:58

What does pos?

n3ss3s

2008-04-27 09:16:06

Hermpie, you should know things like that… Have a look in the SCAR manual.

smithsps

2008-04-27 10:18:10

Updated script

Mainly just changed variables

Thanks
Santa_Clause & Mixster
For helping


Edit: Added 'Pos' definition

Edit2: Added examples.

Da 0wner

2008-04-27 13:21:26

pos is the position of a character in a string.