Jump to content
dopeness

strGet, or its alternative? (getSubStr)

Recommended Posts

Hello, I was wondering how to use the strGet function as I believe it is deprecated. What I am using this for is to make a getWordCount function. If either of these functions exist, please tell me.

 

Here is what I have, and the strGet is returning weird results. (This isn't the full function, because the strGet threw me off so i had to debug):

 

Function getWordCount(s: String) : Integer;
 var a,b,c: integer;
begin
 a:=1;              
 c:=0;
 b:=Length(s);

 For a := 1 to b do
 begin
   writeln(s,a);
 end;
 writeLn('result = '+intToStr(c));

 //the result is defined, i just removed it for this example..
 result:=c;

end;

 

Thanks guys.

Link to comment
Share on other sites

Are you trying to make CountStr function? SCAR Divi 3.34[+] contains CountStr. :)

 

[scar]begin

WriteLn(CountStr('TEST', 'TESTESTEST'));

end.[/scar]

 

Also, here is Count function by me (doesn't work the exactly same way as SCAR's CountStr):

 

[scar]function Count(s, str: string): Integer;

var

l, p: Integer;

begin

l := Length(s);

if ((l > 0) and (l <= Length(str))) then

repeat

p := PosEx(s, str, (p + 1));

if (p > 0) then

Inc(Result);

until (p <= 0);

end;

 

begin

WriteLn(Count('TEST', 'TESTESTEST'));

end.[/scar]

 

AND CountStr2 (works the same way as SCAR's own CountStr):

 

[scar]function CountStr2(s, str: string): Integer;

var

l, p: Integer;

begin

l := Length(s);

if ((l > 0) and (l <= Length(str))) then

repeat

p := PosEx(s, str, (p + 1));

if (p <= 0) then

Break;

Inc(Result);

p := ((p + l) - 1);

until (p <= 0);

end;

 

begin

WriteLn('CountStr2: ' + IntToStr(CountStr2('TEST', 'TESTESTEST')));

WriteLn('CountStr: ' + IntToStr(CountStr('TEST', 'TESTESTEST')));

end.[/scar]

 

Count/CountStr2/CountStr example:

 

[scar]function CountStr2(s, str: string): Integer;

var

l, p: Integer;

begin

l := Length(s);

if ((l > 0) and (l <= Length(str))) then

repeat

p := PosEx(s, str, (p + 1));

if (p <= 0) then

Break;

Inc(Result);

p := ((p + l) - 1);

until (p <= 0);

end;

 

function Count(s, str: string): Integer;

var

l, p: Integer;

begin

l := Length(s);

if ((l > 0) and (l <= Length(str))) then

repeat

p := PosEx(s, str, (p + 1));

if (p > 0) then

Inc(Result);

until (p <= 0);

end;

 

begin

WriteLn('Count: ' + IntToStr(Count('lol', 'lolol lol heylol')));

WriteLn('CountStr2: ' + IntToStr(CountStr2('lol', 'lolol lol heylol')));

WriteLn('CountStr: ' + IntToStr(CountStr('lol', 'lolol lol heylol')));

end.[/scar]

 

..and the last thing, here is CountEx (Contains 2 methods.. cm_CountStr and cm_Count):

 

[scar]function CountEx(s, str: string; count_method: (cm_CountStr, cm_Count)): Integer;

var

l, p: Integer;

begin

l := Length(s);

if ((l > 0) and (l <= Length(str))) then

case count_method of

cm_CountStr: repeat

p := PosEx(s, str, (p + 1));

if (p <= 0) then

Break;

Inc(Result);

p := ((p + l) - 1);

until (p <= 0);

cm_Count: repeat

p := PosEx(s, str, (p + 1));

if (p > 0) then

Inc(Result);

until (p <= 0);

end;

 

end;

 

begin

WriteLn('cm_CountStr: ' + IntToStr(CountEx('TEST', 'TESTESTEST', cm_CountStr)));

WriteLn('cm_Count: ' + IntToStr(CountEx('TEST', 'TESTESTEST', cm_Count)));

end.[/scar]

Edited by Janilabo
Added wiki page for SCAR's CountStr
Link to comment
Share on other sites

So by words do you mean.. Words as in... Word <> single letter (word doesn't equal single letter)?

..or perhaps word <> anything except (empty) space?

 

Edit: AHHHH! I see what you are talking about..

 

[scar]var

i, l: Integer;

s: string;

 

begin

ClearDebug;

s := 'TEST';

l := Length(s);

for i := 1 to l do

WriteLn(IntToStr(i) + '. ' + 's[' + IntToStr(i) + ']: "' + s + '" vs. StrGet(s, ' + IntToStr(i) + '): "' + StrGet(s, i) + '"');

end.[/scar]

 

StrGet seems to be bugging.. I'll report it to MantisBT later.

 

StrGet2:

 

[scar]const

TEST_STR = 'TEST';

 

function StrGet2(S: string; I: LongInt): Char;

begin

if ((Length(S) >= I) and (I > 0)) then

Result := S;

end;

 

var

i, l: Integer;

 

begin

l := Length(TEST_STR);

for i := 1 to l do

WriteLn(StrGet2(TEST_STR, i));

end.[/scar]

Edited by Janilabo
Added "StrGet2()"
Link to comment
Share on other sites

At first i through i just could use the Explode(' ', S) but then i saw that i it would be missleading if you made two spaces between your words. So then i wrote this :-)

 

[sCAR]function CountWords(S: String): Integer;

var

Len, i, ii, Index: Integer;

S2: String;

TSA: TStringArray;

begin

TSA := Explode(' ', S);

for ii := 0 to high(TSA) do begin

for i := low(TSA) to high(TSA) do begin

Index := Pos(TSA, S);

Len := PosEx(' ', S, Index);

S2 := Copy(S, Len + 1, 1);

if S2 = ' ' then

Delete(S, Len, 1);

end;

end;

result := length(Explode(' ', S));

end;

 

begin

writeln('The number of words: ' + IntToStr(CountWords('Hello. Whats up im')));

end.[/sCAR]

  • Confused 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...