Compare length of two strings: Difference between revisions

Content added Content deleted
Line 1: Line 1:
{{task|String manipulation}}
{{task|String manipulation}}
{{basic data operation}}
{{basic data operation}}
[[Category: String manipulation]]
[[Category: String manipulation]]
[[Category:Simple]]
[[Category:Simple]]


;Task:
;Task:
Given two strings of different length, determine which string is longer or shorter. Print both strings and their length, one on each line. Print the longer one first.
Given two strings of different length, determine which string is longer or shorter. Print both strings and their length, one on each line. Print the longer one first.


Measure the length of your string in terms of bytes or characters, as appropriate for your language. If your language doesn't have an operator for measuring the length of a string, note it.
Measure the length of your string in terms of bytes or characters, as appropriate for your language. If your language doesn't have an operator for measuring the length of a string, note it.
Line 13: Line 13:
<br>list = ["abcd","123456789","abcdef","1234567"]
<br>list = ["abcd","123456789","abcdef","1234567"]
<br>Show the strings in descending length order.
<br>Show the strings in descending length order.
{{Template:Strings}}
{{Strings}}
<br><br>
<br><br>


Line 169: Line 169:
<pre>“marché”, byte length = 7, code points: 6
<pre>“marché”, byte length = 7, code points: 6
“marche”, byte length = 6, code points: 6</pre>
“marche”, byte length = 6, code points: 6</pre>

=={{header|Pascal}}==
{{works with|Extended Pascal}}
<lang pascal>program compareLengthOfStrings(output);

const
specimenA = 'RosettaCode';
specimenB = 'Pascal';
specimenC = 'Foobar';
specimenD = 'Pascalish';

type
specimen = (A, B, C, D);
specimens = set of specimen value [];

const
specimenMinimum = A;
specimenMaximum = D;

var
{ the explicit range min..max serves as a safeguard to update max const }
list: array[specimenMinimum..specimenMaximum] of string(24)
value [A: specimenA; B: specimenB; C: specimenC; D: specimenD];
lengthRelationship: array[specimen] of specimens;

procedure analyzeLengths;
var
left, right: specimen;
begin
for left := specimenMinimum to specimenMaximum do
begin
for right := specimenMinimum to specimenMaximum do
begin
if length(list[left]) < length(list[right]) then
begin
lengthRelationship[right] := lengthRelationship[right] + [right]
end
end
end
end;

procedure printSortedByLengths;
var
i: ord(specimenMinimum)..ord(specimenMaximum);
s: specimen;
begin
{ first the string longer than all other strings }
{ lastly print the string not longer than any other string }
for i := ord(specimenMaximum) downto ord(specimenMinimum) do
begin
{ for demonstration purposes: iterate over a set }
for s in [specimenMinimum..specimenMaximum] do
begin
{ card returns the cardinality ("population count") }
if card(lengthRelationship[s]) = i then
begin
writeLn(length(list[s]):8, ' ', list[s])
end
end
end
end;

begin
analyzeLengths;
printSortedByLengths
end.</lang>
{{out}}
<pre> 11 RosettaCode
9 Pascalish
6 Pascal
6 Foobar</pre>


=={{header|Phix}}==
=={{header|Phix}}==