Results 1 to 10 of 10

Thread: Count Function

  1. #1
    Join Date
    Mar 2008
    Location
    Indiana
    Posts
    192
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Count Function

    scar, even though there is probably one.

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

    SCAR Code:
    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:
    SCAR Code:
    function pos(substr, s: string): LongInt;
    //Returns position of substring in string. Returns 0 if not found.

  2. #2
    Join Date
    Mar 2008
    Location
    Indiana
    Posts
    192
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Examples:

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

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

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

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

  3. #3
    Join Date
    Mar 2007
    Posts
    3,116
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

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

  4. #4
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Why would you do this?

  5. #5
    Join Date
    Nov 2006
    Location
    NSW, Australia
    Posts
    3,487
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    SCAR Code:
    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
    [CENTER][img]http://signatures.mylivesignature.com/54486/113/4539C8FAAF3EAB109A3CC1811EF0941B.png[/img][/CENTER]
    [CENTER][BANANA]TSN ~ Vacation! ~ says :I Love Santy[/BANANA][/CENTER]

    [CENTER][BANANA]Raymond - Oh rilie? says :Your smart[/BANANA][/CENTER]

  6. #6
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    You may want to change it so it can count the occurrences of a string. It's easy enough, just change it to:
    SCAR Code:
    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.
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  7. #7
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    What does pos?
    ~Hermen

  8. #8
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

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

  9. #9
    Join Date
    Mar 2008
    Location
    Indiana
    Posts
    192
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Updated script

    Mainly just changed variables

    Thanks
    Santa_Clause & Mixster
    For helping


    Edit: Added 'Pos' definition

    Edit2: Added examples.

  10. #10
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    pos is the position of a character in a string.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Procedure/Function To Count Gold?
    By Dusk412 in forum OSR Help
    Replies: 3
    Last Post: 05-25-2008, 05:27 PM
  2. Replies: 14
    Last Post: 04-16-2008, 04:49 PM
  3. Count DTM
    By marre in forum OSR Help
    Replies: 6
    Last Post: 11-29-2007, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •