dopeness Posted July 12, 2012 Share Posted July 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Janilabo Posted July 13, 2012 Share Posted July 13, 2012 (edited) 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 July 13, 2012 by Janilabo Added wiki page for SCAR's CountStr Quote Link to comment Share on other sites More sharing options...
dopeness Posted July 13, 2012 Author Share Posted July 13, 2012 I'm trying to make a function to count the number of words in a given string. Quote Link to comment Share on other sites More sharing options...
Janilabo Posted July 13, 2012 Share Posted July 13, 2012 (edited) 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 July 13, 2012 by Janilabo Added "StrGet2()" Quote Link to comment Share on other sites More sharing options...
sjesper Posted July 13, 2012 Share Posted July 13, 2012 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] 1 Quote Link to comment Share on other sites More sharing options...
dopeness Posted July 13, 2012 Author Share Posted July 13, 2012 Thanks everyone, i'll do some testing and see how it goes. Quote Link to comment Share on other sites More sharing options...
sjesper Posted July 13, 2012 Share Posted July 13, 2012 Thanks everyone, i'll do some testing and see how it goes. Just found a bug in my function. If you puted some spaces in the beginning of ur string it would count them as words. I have fixed that and pasted the function in here: - http://forums.scar-divi.com/showthread.php?1618-CountWords&p=8590#post8590 Quote Link to comment Share on other sites More sharing options...