PDA

View Full Version : Count Function



smithsps
04-27-2008, 01:13 AM
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
04-27-2008, 01:49 AM
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
04-27-2008, 02:01 AM
Oh I thought your function did something different so I posted the scar function.

Da 0wner
04-27-2008, 04:08 AM
Why would you do this?

Santa_Clause
04-27-2008, 08:18 AM
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
04-27-2008, 10:19 AM
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
04-27-2008, 10:23 AM
What does pos?

n3ss3s
04-27-2008, 01:16 PM
Hermpie, you should know things like that... Have a look in the SCAR manual.

smithsps
04-27-2008, 02:18 PM
Updated script

Mainly just changed variables

Thanks
Santa_Clause & Mixster
For helping


Edit: Added 'Pos' definition

Edit2: Added examples.

Da 0wner
04-27-2008, 05:21 PM
pos is the position of a character in a string.