Compare length of two strings: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 15:
{{Strings}}
<br><br>
 
=={{header|Ada}}==
Ada provides three string types: string, bounded_string and unbounded_string. I chose to use the string type, which is a fixed length array of strings. The Ada string type is declared to be an unconstrained array of character, allowing different instances of string to be different sizes. Ada array elements must all be of the same size. The list of strings is therefore an array of string access, which is similar to string references.
<lang ada>--------------
-- compare the length of strings
--------------
 
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Constrained_Array_Sort;
 
procedure Main is
type Str_access is access all String;
subtype idx is Integer range 1 .. 4;
type str_list is array (idx) of Str_access;
 
function "<" (Left, Right : Str_access) return Boolean is
begin
-- ordered to sort from greatest to least
return Right.all'Length < Left.all'Length;
end "<";
 
procedure sort is new Ada.Containers.Generic_Constrained_Array_Sort
(Index_Type => idx, Element_Type => Str_access, Array_Type => str_list);
 
List : str_list :=
(new String'("abcd"), new String'("123456789"), new String'("abcdef"),
new String'("1234567"));
begin
sort (List);
for S of List loop
Put_Line
('"' & S.all & '"' & " has a length of" &
Integer'Image (S.all'Length));
end loop;
 
end Main;</lang>
{{out}}
<pre>
"123456789" has a length of 9
"1234567" has a length of 7
"abcdef" has a length of 6
"abcd" has a length of 4
</pre>
 
=={{header|ALGOL 68}}==
82

edits