Compare length of two strings: Difference between revisions
Content added Content deleted
VincentARM (talk | contribs) (add task to aarch64 assembly raspberry pi 3) |
No edit summary |
||
Line 1,128: | Line 1,128: | ||
3 for |
3 for |
||
</pre> |
</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|Classes,StdCtrls,SysUtils}} |
|||
Uses the Standard Delphi component TList compile the list of pointers to the strings and then sort them using a custom sort comparison. |
|||
<syntaxhighlight lang="Delphi"> |
|||
var SA1: array [0..1] of string = ('Very Long String','short string'); |
|||
var SA2: array [0..11] of string = ('Like','sands','through','the','hourglass', |
|||
'these','are','the','days','of','our','lives'); |
|||
function Compare(P1,P2: pointer): integer; |
|||
{Compare for quick sort} |
|||
begin |
|||
Result:=Length(PString(P2)^)-Length(PString(P1)^); |
|||
end; |
|||
procedure ShowStringLengths(SA: array of string; Memo: TMemo); |
|||
{Sort strings by length and display string and length} |
|||
var List: TList; |
|||
var I: integer; |
|||
var S: string; |
|||
begin |
|||
List:=TList.Create; |
|||
try |
|||
for I:=0 to High(SA) do |
|||
List.Add(@SA[I]); |
|||
List.Sort(Compare); |
|||
for I:=0 to List.Count-1 do |
|||
begin |
|||
S:=PString(List[I])^; |
|||
Memo.Lines.Add(IntToStr(Length(S))+': '+S); |
|||
end; |
|||
finally List.Free; end; |
|||
end; |
|||
procedure SortedStringLists(Memo: TMemo); |
|||
{Test two different string arrays} |
|||
begin |
|||
Memo.Lines.Add('Two word test: '); |
|||
Memo.Lines.Add(''); |
|||
ShowStringLengths(SA1,Memo); |
|||
Memo.Lines.Add(''); |
|||
Memo.Lines.Add('Twelve word test: '); |
|||
Memo.Lines.Add(''); |
|||
ShowStringLengths(SA2,Memo); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Two word test: |
|||
16: Very Long String |
|||
12: short string |
|||
Twelve word test: |
|||
9: hourglass |
|||
7: through |
|||
5: lives |
|||
5: these |
|||
5: sands |
|||
4: days |
|||
4: Like |
|||
3: our |
|||
3: are |
|||
3: the |
|||
3: the |
|||
2: of |
|||
</pre> |
|||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |