Compare length of two strings: Difference between revisions

m
no edit summary
mNo edit summary
(125 intermediate revisions by 54 users not shown)
Line 15:
{{Strings}}
<br><br>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program complength64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeConstantesARM64.inc"
 
/************************************/
/* structures */
/************************************/
.struct 0
list_string: // string address
.struct list_string + 8
list_length: // string length
.struct list_length + 8
list_end:
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "@ length : @\n"
szCarriageReturn: .asciz "\n"
szLibSort: .asciz "\nAfter sort\n"
szString1: .asciz "abcd"
szString2: .asciz "123456789"
szString3: .asciz "abcdef"
szString4: .asciz "1234567"
 
.align 4
tabStrings: .quad szString1 // string address array
.quad 0
.quad szString2
.quad 0
.quad szString3
.quad 0
.quad szString4
.quad 0
.equ NBTABSTRINGS, (. - tabStrings) / list_end // compute items number
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x4,qAdrtabStrings // string array address
mov x5,#0 // indice
mov x6,#list_end // structure size
1: // item loop
madd x3,x5,x6,x4 // compute item address
ldr x0,[x3,#list_string] // load string address
bl stringRoutine // length string compute
str x0,[x3,#list_length] // store result in array
add x5,x5,#1 // increment indice
cmp x5,#NBTABSTRINGS // end ?
blt 1b // no -> loop
mov x0,x4 // string array address
mov x1,#0 // first item
mov x2,#NBTABSTRINGS // item number
bl insertionSort // sort
ldr x0,qAdrszLibSort
bl affichageMess
mov x0,x4 // string array address
mov x5,#0 // indice
mov x6,#list_end
2: // item loop
madd x3,x5,x6,x4
ldr x0,[x3,#list_string]
bl stringRoutine // use same routine for display result after sort
add x5,x5,#1
cmp x5,#NBTABSTRINGS // end ?
blt 2b // no -> loop
 
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessResult: .quad szMessResult
qAdrsZoneConv: .quad sZoneConv
qAdrtabStrings: .quad tabStrings
qAdrszLibSort: .quad szLibSort
/***************************************************/
/* string exec */
/***************************************************/
// x0 contains string address
// x0 return length
stringRoutine:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x3,x0 // save string address
mov x1,x0
ldr x0,qAdrszMessResult
bl strInsertAtCharInc // insert string in result message
mov x2,x0 // save new message address
mov x0,x3 // restaur string address
bl stringlength // compute length
mov x3,x0
ldr x1,qAdrsZoneConv
bl conversion10 // call decimal conversion
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
bl affichageMess // display result message
mov x0,x3
100:
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* compute string length */
/***************************************************/
// x0 contains string address
stringlength:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x1,#-1 // init counter
1: // loop
add x1,x1,#1 // increment counter
ldrb w2,[x0,x1] // load byte string
cmp w2,#0 // zero final ?
bne 1b // no -> loop
mov x0,x1 // return length
100:
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
 
/******************************************************************/
/* insertion sort */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element */
/* x2 contains the number of element */
insertionSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
stp x10,x11,[sp,-16]! // save registers
mov x6,x0
mov x7,#list_end
add x3,x1,#1 // start index i
1: // start loop
madd x8,x7,x3,x6
ldr x10,[x8,#list_length] // load value A[i]
ldr x0,[x8,#list_string] // load string address A[i]
sub x5,x3,#1 // index j
2:
madd x9,x7,x5,x6
ldr x4,[x9,#list_length] // load value A[j]
cmp x4,x10 // compare value
bge 3f
add x5,x5,#1 // increment index j
madd x8,x7,x5,x6
str x4,[x8,#list_length] // store value A[j+1]
ldr x4,[x9,#list_string] // load string address
str x4,[x8,#list_string] // store string address
subs x5,x5,#2 // j = i - 1
cmp x5,x1 // compare with first item
bge 2b // loop if j >= first item
3:
add x5,x5,#1 // increment index j
madd x9,x7,x5,x6
str x10,[x9,#list_length] // store value A[i] in A[j+1]
str x0,[x9,#list_string] // and store string address
add x3,x3,#1 // increment index i
cmp x3,x2 // end ?
blt 1b // no -> loop
 
100:
ldp x10,x11,[sp],16 // restaur registers
ldp x8,x9,[sp],16 // restaur registers
ldp x6,x7,[sp],16 // restaur registers
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
abcd length : 4
123456789 length : 9
abcdef length : 6
1234567 length : 7
 
After sort
123456789 length : 9
1234567 length : 7
abcdef length : 6
abcd length : 4
</pre>
=={{header|Ada}}==
 
<syntaxhighlight lang="ada">with ada.command_line, ada.containers.indefinite_vectors, ada.text_io;
procedure compare_lengths is
package string_vector is new ada.containers.indefinite_vectors
(index_type => Positive, element_type => String);
 
function "<" (left, right : String) return Boolean is
begin
return left'length > right'length;
end "<";
 
package string_vector_sorting is new string_vector.generic_sorting;
list : string_vector.Vector;
begin
for i in 1 .. ada.command_line.argument_count loop
list.append (ada.command_line.argument (i));
end loop;
string_vector_sorting.sort (list);
for elem of list loop
ada.text_io.put_line (elem'length'image & ": " & elem);
end loop;
end compare_lengths;
</syntaxhighlight>
{{out}}
./compare_lengths Like sands through the hourglass these are the days of our lives
<pre>
9: hourglass
7: through
5: lives
5: sands
5: these
4: days
4: Like
3: are
3: our
3: the
3: the
2: of
</pre>
 
=={{header|ALGOL 68}}==
Algol 68 does not have an in-built "LENGTH" operator, it does have operators LWB and UPB which return the lower bound and upper bound of an array and as strings are arrays of characters, LENGTH can easily be constructed from these.<br>
In most Algol 68 implementations such as Algol 68G and Rutgers Algol 68, the CHAR type is an 8-bit byte.
<langsyntaxhighlight lang="algol68">BEGIN # compare string lengths #
# returns the length of s using the builtin UPB and LWB operators #
OP LENGTH = ( STRING s )INT: ( UPB s + 1 ) - LWB s;
Line 30 ⟶ 280:
print string( not shorter );
IF LENGTH shorter <= LENGTH not shorter THEN print string( shorter ) FI
END</langsyntaxhighlight>
{{out}}
<pre>
Line 37 ⟶ 287:
</pre>
 
=={{header|FreeBASICAPL}}==
For a good intro to APL, see [https://archive.org/details/apl-2-at-a-glance-brown-pakin-polivka/mode/2up APL2 At A Glance]
<lang freebasic>sub comp( A as string, B as string )
<syntaxhighlight lang="apl">
sv ← 'defg' 'hijklm' 'abc' 'abcd'
⍉(⍴¨sv[⍒sv]),[0.5]sv[⍒sv]
6 hijklm
4 defg
4 abcd
3 abc
</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI or android 32 bits */
/* program complength.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
 
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
/************************************/
/* structures */
/************************************/
.struct 0
list_string: @ string address
.struct list_string + 4
list_length: @ string length
.struct list_length + 4
list_end:
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "@ length : @\n"
szCarriageReturn: .asciz "\n"
szLibSort: .asciz "\nAfter sort\n"
szString1: .asciz "abcd"
szString2: .asciz "123456789"
szString3: .asciz "abcdef"
szString4: .asciz "1234567"
 
.align 4
tabStrings: .int szString1 @ string address array
.int 0
.int szString2
.int 0
.int szString3
.int 0
.int szString4
.int 0
.equ NBTABSTRINGS, (. - tabStrings) / list_end @ compute items number
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r4,iAdrtabStrings @ string array address
mov r5,#0 @ indice
mov r6,#list_end @ structure size
1: @ item loop
mla r3,r5,r6,r4 @ compute item address
ldr r0,[r3,#list_string] @ load string address
bl stringRoutine @ length string compute
str r0,[r3,#list_length] @ store result in array
add r5,#1 @ increment indice
cmp r5,#NBTABSTRINGS @ end ?
blt 1b @ no -> loop
mov r0,r4 @ string array address
mov r1,#0 @ first item
mov r2,#NBTABSTRINGS @ item number
bl insertionSort @ sort
ldr r0,iAdrszLibSort
bl affichageMess
mov r0,r4 @ string array address
mov r5,#0 @ indice
mov r6,#list_end
2: @ item loop
mla r3,r5,r6,r4
ldr r0,[r3,#list_string]
bl stringRoutine @ use same routine for display result after sort
add r5,#1
cmp r5,#NBTABSTRINGS @ end ?
blt 2b @ no -> loop
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessResult: .int szMessResult
iAdrsZoneConv: .int sZoneConv
iAdrtabStrings: .int tabStrings
iAdrszLibSort: .int szLibSort
/***************************************************/
/* string exec */
/***************************************************/
// r0 contains string address
// r0 return length
stringRoutine:
push {r1-r3,lr} @ save registers
mov r3,r0 @ save string address
mov r1,r0
ldr r0,iAdrszMessResult
bl strInsertAtCharInc @ insert string in result message
mov r2,r0 @ save new message address
mov r0,r3 @ restaur string address
bl stringlength @ compute length
mov r3,r0
ldr r1,iAdrsZoneConv
bl conversion10 @ call decimal conversion
mov r0,r2
ldr r1,iAdrsZoneConv @ insert conversion in message
bl strInsertAtCharInc
bl affichageMess @ display result message
mov r0,r3
100:
pop {r1-r3,pc} @ restaur registers
/***************************************************/
/* compute string length */
/***************************************************/
// r0 contains string address
stringlength:
push {r1-r2,lr} @ save registers
mov r1,#-1 @ init counter
1: @ loop
add r1,#1 @ increment counter
ldrb r2,[r0,r1] @ load byte string
cmp r2,#0 @ zero final ?
bne 1b @ no -> loop
mov r0,r1 @ return length
100:
pop {r1-r2,pc} @ restaur registers
/******************************************************************/
/* insertion sort */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the first element */
/* r2 contains the number of element */
insertionSort:
push {r1-r10,lr} @ save registers
mov r6,r0
mov r7,#list_end
add r3,r1,#1 @ start index i
1: @ start loop
mla r8,r7,r3,r6
ldr r10,[r8,#list_length] @ load value A[i]
ldr r0,[r8,#list_string] @ load string address A[i]
sub r5,r3,#1 @ index j
2:
mla r9,r7,r5,r6
ldr r4,[r9,#list_length] @ load value A[j]
cmp r4,r10 @ compare value
bge 3f
add r5,#1 @ increment index j
mla r8,r7,r5,r6
str r4,[r8,#list_length] @ store value A[j+1]
ldr r4,[r9,#list_string] @ load string address
str r4,[r8,#list_string] @ store string address
subs r5,#2 @ j = i - 1
cmp r5,r1 @ compare with first item
bge 2b @ loop if j >= first item
3:
add r5,#1 @ increment index j
mla r9,r7,r5,r6
str r10,[r9,#list_length] @ store value A[i] in A[j+1]
str r0,[r9,#list_string] @ and store string address
add r3,#1 @ increment index i
cmp r3,r2 @ end ?
blt 1b @ no -> loop
 
100:
pop {r1-r10,lr}
bx lr
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
abcd length : 4
123456789 length : 9
abcdef length : 6
1234567 length : 7
 
After sort
123456789 length : 9
1234567 length : 7
abcdef length : 6
abcd length : 4
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">sortByLength: function [strs][
map sort.descending.by:'v
map strs 'str -> #[s: str, v: size str]
'z -> z\s
]
 
A: "I am string"
B: "I am string too"
 
sA: size A
sB: size B
 
if? sA < sB ->
print ["string ->" A "(" sA ") is smaller than string ->" B "(" sB ")"]
else [
if? sA > sB ->
print ["string ->" A "(" sA ") is larger than string ->" B "(" sB ")"]
else ->
print ["string ->" A "(" sA ") and string ->" B "(" sB ") are of equal length"]
]
 
print ["sorted strings (by length):" sortByLength ["abcd" "123456789" "abcdef" "1234567"]]</syntaxhighlight>
 
{{out}}
 
<pre>string -> I am string ( 11 ) is smaller than string -> I am string too ( 15 )
sorted strings (by length): [123456789 1234567 abcdef abcd]</pre>
 
=={{header|Asymptote}}==
<syntaxhighlight lang="asymptote">string A, B, t = '\t';
 
void comp(string A, string B) {
if (length(A) >= length(B)) {
write(A+t, length(A));
write(B+t, length(B));
} else {
write(B+t, length(B));
write(A+t, length(A));
}
}
 
comp("abcd", "123456789");</syntaxhighlight>
{{out}}
<pre>123456789 9
abcd 4</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">list := ["abcd","123456789","abcdef","1234567"]
 
sorted := []
for i, s in list
sorted[0-StrLen(s), s] := s
for l, obj in sorted
{
i := A_Index
for s, v in obj
{
if (i = 1)
result .= """" s """ has length " 0-l " and is the longest string.`n"
else if (i < sorted.Count())
result .= """"s """ has length " 0-l " and is neither the longest nor the shortest string.`n"
else
result .= """"s """ has length " 0-l " and is the shorted string.`n"
}
}
MsgBox % result</syntaxhighlight>
{{out}}
<pre>"123456789" has length 9 and is the longest string.
"1234567" has length 7 and is neither the longest nor the shortest string.
"abcdef" has length 6 and is neither the longest nor the shortest string.
"abcd" has length 4 and is the shorted string.</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f COMPARE_LENGTH_OF_TWO_STRINGS.AWK
BEGIN {
main("abcd","123456789")
main("longer","short")
main("hello","world")
exit(0)
}
function main(Sa,Sb, La,Lb) {
La = length(Sa)
Lb = length(Sb)
if (La > Lb) {
printf("a>b\n%3d %s\n%3d %s\n\n",La,Sa,Lb,Sb)
}
else if (La < Lb) {
printf("a<b\n%3d %s\n%3d %s\n\n",Lb,Sb,La,Sa)
}
else {
printf("a=b\n%3d %s\n%3d %s\n\n",Lb,Sb,La,Sa)
}
}
</syntaxhighlight>
{{out}}
<pre>
a<b
9 123456789
4 abcd
 
a>b
6 longer
5 short
 
a=b
5 world
5 hello
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
Printing CHR$(14) does nothing by default in Applesoft BASIC. Commodore BASIC appends spaces to numbers, but otherwise the [[Compare_length_of_two_strings#Commodore_BASIC]] code works the same in Applesoft BASIC.
{{out}}
<pre>*** (1) TWO STRINGS ***
LONGER STRING (13)
SHORT STRING (12)
 
 
*** (2) MORE THAN 2 STRINGS***
SHE DOESN'T STUDY GERMAN ON MONDAY (34)
EVERY CHILD LIKES AN ICE CREAM (30)
THE COURSE STARTS NEXT SUNDAY (29)
DOES SHE LIVE IN PARIS? (23)
SHE SWIMS EVERY MORNING (23)
THE EARTH IS SPHERICAL (22)
WE SEE THEM EVERY WEEK (22)
HE DOESN'T TEACH MATH (21)
CATS HATE WATER (15)
I LIKE TEA (10)
 
</pre>
==={{header|Commodore BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Commodore BASIC|2.0}}
{{works with|Commodore BASIC|3.5}}
<syntaxhighlight lang="gwbasic">0 REM ROSETTACODE.ORG
1 REM COMPARE LENGTH OF TWO STRINGS
2 REM GIVEN TWO STRINGS OF DIFFERENT
3 REM LENGTH, DETERMINE WHICH STRING IS
4 REM LONGER OR SHORTER.
5 REM PRINT BOTH STRINGS AND THEIR
6 REM LENGTH, ONE ON EACH LINE. PRINT
7 REM THE LONGER ONE FIRST.
8 REM
9 REM ********************************
10 REM
20 REM PRINT CHR$(14): REM CHANGE TO LOWER/UPPER CASE CHAR SET
30 GOSUB 200: REM 1 - COMPARE LENGTH OF 2 STRINGS
40 GOSUB 300: REM 2- MORE THAN 2 STRINGS
50 END
200 PRINT"*** (1) TWO STRINGS ***"
210 A$ = "SHORT STRING"
220 B$ = "LONGER STRING"
230 A = LEN(A$)
240 B = LEN(B$)
250 IF A>B THEN PRINT A$;" (";A;")": PRINT B$;" (";B;")"
260 IF A<=B THEN PRINT B$;" (";B;")": PRINT A$;" (";A;")"
270 PRINT: PRINT
280 RETURN
300 PRINT"*** (2) MORE THAN 2 STRINGS***"
310 DIM C$(100)
320 N = 0
330 READ A$
340 IF A$ = "$$$" THEN 400
350 N = N+1
360 C$(N) = A$
370 IF N = 100 THEN 400
380 GOTO 330
390 REM SORT THE STRINGS
400 FOR J=1 TO N-1
410 FOR I=1 TO N-J
420 IF LEN(C$(I)) < LEN(C$(I+1)) THEN A$=C$(I): C$(I)=C$(I+1): C$(I+1)=A$
430 NEXT
440 NEXT
450 REM PRINT OUT THE STRINGS
460 FOR I=1 TO N
470 PRINT C$(I);" (";LEN(C$(I));")"
480 NEXT
490 PRINT: PRINT
500 RETURN
1000 DATA "DOES SHE LIVE IN PARIS?"
1010 DATA "HE DOESN'T TEACH MATH"
1020 DATA "CATS HATE WATER"
1030 DATA "SHE DOESN'T STUDY GERMAN ON MONDAY"
1040 DATA "EVERY CHILD LIKES AN ICE CREAM"
1050 DATA "THE EARTH IS SPHERICAL"
1060 DATA "THE COURSE STARTS NEXT SUNDAY"
1070 DATA "SHE SWIMS EVERY MORNING"
1080 DATA "I LIKE TEA"
1090 DATA "WE SEE THEM EVERY WEEK"
1100 DATA "$$$"</syntaxhighlight>
 
{{out}}
<pre>
*** (1) TWO STRINGS ***
LONGER STRING ( 13 )
SHORT STRING ( 12 )
 
 
*** (2) MORE THAN 2 STRINGS***
SHE DOESN'T STUDY GERMAN ON MONDAY ( 34
)
EVERY CHILD LIKES AN ICE CREAM ( 30 )
THE COURSE STARTS NEXT SUNDAY ( 29 )
DOES SHE LIVE IN PARIS? ( 23 )
SHE SWIMS EVERY MORNING ( 23 )
THE EARTH IS SPHERICAL ( 22 )
WE SEE THEM EVERY WEEK ( 22 )
HE DOESN'T TEACH MATH ( 21 )
CATS HATE WATER ( 15 )
I LIKE TEA ( 10 )
</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">subroutine comp(A$, B$)
if length(A$) >= length(B$) then
print A$, length(A$)
print B$, length(B$)
else
print B$, length(B$)
print A$, length(A$)
end if
end subroutine
 
call comp("abcd", "123456789")</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
The [[#True BASIC|True BASIC]] solution works without any changes.
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Compare("abcd", "123456789")
End
 
Sub Compare(A As String, B As String)
 
If Len(A) >= Len(B) Then
Print A, Len(A)
Print B, Len(B)
Else
Print B, Len(B)
Print A, Len(A)
End If
 
End Sub</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|True BASIC}}
<syntaxhighlight lang="basic">
10 'SAVE "SSTRING",A
20 ' Length of a group of strings
30 OPTION BASE 1
40 DIM SSTRING$(10)
50 I=0: J=1
60 ' Begin of program cycle
70 CLS
80 PRINT "This program shows the length of up to 10 captured strings."
90 PRINT "Enter an empty string to finish anytime."
100 PRINT
110 ' Do
120 PRINT "Capture the string";STR$(J);": ";
130 INPUT "", SSTRING$(J)
140 IF SSTRING$(J)="" THEN 180 ' Exit Do
150 IF J>1 THEN GOSUB 280 ' Autosort
160 J = J + 1
170 IF J < 11 THEN 110 ' Loop
180 ' Show results
190 CLS
200 J = J - 1
210 IF J<1 THEN PRINT "You entered no strings.": GOTO 260
220 PRINT "You entered";J;"strings. Lengths are as follows:"
230 FOR I=1 TO J
240 PRINT USING "##. &: ## chars."; I;SSTRING$(I);LEN(SSTRING$(I))
250 NEXT I
260 PRINT: PRINT "End of program execution."
270 END
280 ' Autosort subroutine
290 I=J
300 WHILE I>1
310 IF LEN(SSTRING$(I)) > LEN(SSTRING$(I-1)) THEN SWAP SSTRING$(I), SSTRING$(I-1)
320 I=I-1
330 WEND
340 RETURN
</syntaxhighlight>
{{out}}
The user enters a list of up to 10 strings
<pre>
This program shows the length of up to 10 captured strings.
Enter an empty string to finish anytime.
 
Capture the string 1: abcd
Capture the string 2: 123456789
Capture the string 3: abcdef
Capture the string 4: 1234567
Capture the string 5:
 
You entered 4 strings. Lengths are as follows:
1. 123456789: 9 chars.
2. 1234567: 7 chars.
3. abcdef: 6 chars.
4. abcd: 4 chars.
 
End of program execution.
</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|Just BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|True BASIC}}
{{trans|True BASIC}}
<syntaxhighlight lang="qbasic">100 LET A$ = "abcd"
110 LET B$ = "123456789"
120 GOSUB 140
130 END
140 REM SUB comp(A$, B$)
150 IF LEN(A$) >= LEN(B$) THEN PRINT A$,LEN(A$) : PRINT B$,LEN(B$)
180 IF LEN(A$) < LEN(B$) THEN PRINT B$,LEN(B$) : PRINT A$,LEN(A$)
220 RETURN</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure comp(A.s, B.s)
If Len(A) >= Len(B)
PrintN(A + #TAB$ + Str(Len(A)))
PrintN(B + #TAB$ + Str(Len(B)))
Else
PrintN(B + #TAB$ + Str(Len(B)))
PrintN(A + #TAB$ + Str(Len(A)))
EndIf
EndProcedure
 
OpenConsole()
comp("abcd", "123456789")
Input()
CloseConsole()</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Chipmunk Basic}}
{{works with|True BASIC}}
<syntaxhighlight lang="qbasic">SUB comp(A$, B$)
IF LEN(A$) >= LEN(B$) THEN
PRINT A$, LEN(A$)
PRINT B$, LEN(B$)
ELSE
PRINT B$, LEN(B$)
PRINT A$, LEN(A$)
END IF
END SUB
 
CALL comp("abcd", "123456789")
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">sub comp A$, B$
if len(A$) >= len(B$) then
print A$; chr$(9); len(A$)
print B$; chr$(9); len(B$)
else
print B$; chr$(9); len(B$)
print A$; chr$(9); len(A$)
end if
end sub
 
call comp "abcd", "123456789"</syntaxhighlight>
 
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
func compfun(x, y)
if(len(x) == len(y))
return 0
elseif(len(x) > len(y))
return -1
else
return 1
endif
end
 
list = ["abcd","123456789","abcdef","1234567"]
 
sort list use compfun(x, y)
 
for s in list
print s, len(s)
next
</syntaxhighlight>
 
 
==={{header|True BASIC}}===
{{works with|QBasic}}
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">SUB comp(A$, B$)
IF LEN(A$) >= LEN(B$) THEN
PRINT A$, LEN(A$)
PRINT B$, LEN(B$)
ELSE
PRINT B$, LEN(B$)
PRINT A$, LEN(A$)
END IF
END SUB
 
CALL comp("abcd", "123456789")
END</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Compare length of two strings"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION comp (A$, B$)
 
 
FUNCTION Entry ()
comp("abcd", "123456789")
END FUNCTION
 
FUNCTION comp (A$, B$)
IF LEN(A$) >= LEN(B$) THEN
PRINT A$, LEN(A$)
PRINT B$, LEN(B$)
ELSE
PRINT B$, LEN(B$)
PRINT A$, LEN(A$)
END IF
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub comp(A$, B$)
if len(A$) >= len(B$) then
print A$, chr$(9), len(A$)
print B$, chr$(9), len(B$)
else
print B$, chr$(9), len(B$)
print A$, chr$(9), len(A$)
end if
end sub
 
comp("abcd", "123456789")</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">sub comp( A as string, B as string )
if len(A)>=len(B) then
print A, len(A)
Line 48 ⟶ 964:
end sub
 
comp( "abcd", "123456789" )</langsyntaxhighlight>
{{out}}<pre>123456789 9
abcd 4</pre>
 
=={{header|BQN}}==
BQN's grade functions(similar to APL) produces the indices to sort an array. We grade the lengths, then use those to arrange the strings correctly.
 
<syntaxhighlight lang="bqn">Compare ← >·(⍒⊑¨)⊸⊏≠⊸⋈¨
 
•Show Compare ⟨"hello", "person"⟩
•Show Compare ⟨"abcd", "123456789", "abcdef", "1234567"⟩</syntaxhighlight>
<syntaxhighlight lang="text">┌─
╵ 6 "person"
5 "hello"
┌─
╵ 9 "123456789"
7 "1234567"
6 "abcdef"
4 "abcd"
┘</syntaxhighlight>
 
=={{header|C}}==
{{Works with|C11}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int cmp(const int* a, const int* b)
{
return *b - *a; // reverse sort!
}
 
void compareAndReportStringsLength(const char* strings[], const int n)
{
if (n > 0)
{
char* has_length = "has length";
char* predicate_max = "and is the longest string";
char* predicate_min = "and is the shortest string";
char* predicate_ave = "and is neither the longest nor the shortest string";
 
int* si = malloc(2 * n * sizeof(int));
if (si != NULL)
{
for (int i = 0; i < n; i++)
{
si[2 * i] = strlen(strings[i]);
si[2 * i + 1] = i;
}
qsort(si, n, 2 * sizeof(int), cmp);
 
int max = si[0];
int min = si[2 * (n - 1)];
 
for (int i = 0; i < n; i++)
{
int length = si[2 * i];
char* string = strings[si[2 * i + 1]];
char* predicate;
if (length == max)
predicate = predicate_max;
else if (length == min)
predicate = predicate_min;
else
predicate = predicate_ave;
printf("\"%s\" %s %d %s\n",
string, has_length, length, predicate);
}
 
free(si);
}
else
{
fputs("unable allocate memory buffer", stderr);
}
}
}
 
int main(int argc, char* argv[])
{
char* list[] = { "abcd", "123456789", "abcdef", "1234567" };
 
compareAndReportStringsLength(list, 4);
 
return EXIT_SUCCESS;
}</syntaxhighlight>
{{output}}
<pre>
"123456789" has length 9 and is the longest string
"1234567" has length 7 and is neither the longest nor the shortest string
"abcdef" has length 6 and is neither the longest nor the shortest string
"abcd" has length 4 and is the shortest string
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <algorithm>
#include <string>
#include <list>
 
using namespace std;
 
bool cmp(const string& a, const string& b)
{
return b.length() < a.length(); // reverse sort!
}
 
void compareAndReportStringsLength(list<string> listOfStrings)
{
if (!listOfStrings.empty())
{
char Q = '"';
string has_length(" has length ");
string predicate_max(" and is the longest string");
string predicate_min(" and is the shortest string");
string predicate_ave(" and is neither the longest nor the shortest string");
 
list<string> ls(listOfStrings); // clone to avoid side-effects
ls.sort(cmp);
int max = ls.front().length();
int min = ls.back().length();
 
for (list<string>::iterator s = ls.begin(); s != ls.end(); s++)
{
int length = s->length();
string* predicate;
if (length == max)
predicate = &predicate_max;
else if (length == min)
predicate = &predicate_min;
else
predicate = &predicate_ave;
 
cout << Q << *s << Q << has_length << length << *predicate << endl;
}
}
}
 
int main(int argc, char* argv[])
{
list<string> listOfStrings{ "abcd", "123456789", "abcdef", "1234567" };
compareAndReportStringsLength(listOfStrings);
 
return EXIT_SUCCESS;
}</syntaxhighlight>
 
{{output}}
<pre>"123456789" has length 9 and is the longest string
"1234567" has length 7 and is neither the longest nor the shortest string
"abcdef" has length 6 and is neither the longest nor the shortest string
"abcd" has length 4 and is the shortest string
</pre>
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|10+}}
<syntaxhighlight lang="csharp">
void WriteSorted(string[] strings)
{
var sorted = strings.OrderByDescending(x => x.Length);
foreach(var s in sorted) Console.WriteLine($"{s.Length}: {s}");
}
WriteSorted(new string[] { "abcd", "123456789", "abcdef", "1234567" });
</syntaxhighlight>
{{output}}
<pre>
9: 123456789
7: 1234567
6: abcdef
4: abcd
</pre>
 
=={{header|CFEngine}}==
<syntaxhighlight lang="cfengine3">
bundle agent __main__
{
vars:
"strings" slist => { "abcd", "123456789", "abcdef", "1234567" };
 
"sorted[$(with)]"
string => "$(strings)",
with => string_length( "$(strings)" );
 
"sort_idx" slist => reverse( sort( getindices( "sorted" ), lex ) );
 
reports:
"'$(sorted[$(sort_idx)])' is $(sort_idx) characters in length.";
}
</syntaxhighlight>
 
{{output}}
<pre>
R: '123456789' is 9 characters in length.
R: '1234567' is 7 characters in length.
R: 'abcdef' is 6 characters in length.
R: 'abcd' is 4 characters in length.
</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defun sort-and-print-strings (strings)
(dolist (s (sort (copy-list strings) #'> :key #'length))
(format t "~A ~A~%" (length s) s)))
 
(sort-and-print-strings '("Lisp" "stands" "for" "List" "Processing"))
</syntaxhighlight>
{{out}}
<pre>
10 Processing
6 stands
4 Lisp
4 List
3 for
</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}}==
<syntaxhighlight lang="easylang">
proc scmp a$ b$ . .
if len a$ < len b$
swap a$ b$
.
print a$ & " - " & len a$
print b$ & " - " & len b$
print ""
.
scmp "Easy" "Language"
scmp "Rosetta" "Code"
</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun sort-list-by-string-length (list-of-strings)
"Order LIST-OF-STRINGS from longest to shortest."
(sort list-of-strings 'longer-string)) ; sort by "longer-string" function below
 
(defun longer-string (string-1 string-2)
"Test if STRING-1 is longer than STRING-2."
(> (length string-1) (length string-2))) ; is STRING-1 longer than STRING-2?
</syntaxhighlight>
{{out}}
(sort-list-by-string-length '("abcd" "123456789" "abcdef" "1234567"))
<pre>
("123456789" "1234567" "abcdef" "abcd")
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
List list = text["abcd","123456789","abcdef","1234567", "Привет, мир"]
^|this solves the task doing the comparison by using the diamond operator|^
fun comparator = int by text a, text b do return b.length <> a.length end
List sorted = list.sort(comparator)
writeLine("text".padEnd(15, " ") + "units".padStart(6, " ") + "bytes".padStart(6, " "))
for each text value in sorted
writeLine(value.padEnd(15, " ") +
(text!value.length).padStart(6, " ") +
^|conversion from text to blob uses utf8 encoding|^
(text!(blob!value).length).padStart(6, " "))
end
</syntaxhighlight>
{{out}}
<pre>
text units bytes
Привет, мир 11 20
123456789 9 9
1234567 7 7
abcdef 6 6
abcd 4 4
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<syntaxhighlight lang=factor>USING: formatting io kernel qw sequences sorting ;
 
: .length ( str -- ) dup length "%u has length %d\n" printf ;
 
"I am a string" "I am a string too"
[ longer .length ] [ shorter .length ] 2bi nl
 
qw{ abcd 123456789 abcdef 1234567 } dup [ length ] inv-sort-with
"%u sorted by descending length:\n%u\n" printf</syntaxhighlight>
{{out}}
<pre>
"I am a string too" has length 17
"I am a string" has length 13
 
{ "abcd" "123456789" "abcdef" "1234567" } sorted by descending length:
{ "123456789" "1234567" "abcdef" "abcd" }
</pre>
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
Traditionally, Forth strings are "counted strings" (as opposed to null-terminated strings like C). So, there is no need to search the length, we just need to swap strings if necessary before printing.
<syntaxhighlight lang="Forth">
: say dup . ." : " type ;
: comp-strings
2 pick over
> if 2swap then
cr say cr say cr
;
 
</syntaxhighlight>
{{out}}<pre>s" shortest string" s" longest string" comp-strings
15 : shortest string
14 : longest string
ok
s" longest string" s" shortest string" comp-strings
15 : shortest string
14 : longest string
ok
</pre>
 
=={{header|Fortran}}==
Normally would use an external library for sorting, but to
remain self-contained, created (a very inefficient) sort_int()
procedure.
<syntaxhighlight lang="fortran">
program demo_sort_indexed
implicit none
 
call print_sorted_by_length( [character(len=20) :: "shorter","longer"] )
call print_sorted_by_length( [character(len=20) :: "abcd","123456789","abcdef","1234567"] )
call print_sorted_by_length( [character(len=20) :: 'the','quick','brown','fox','jumps','over','the','lazy','dog'])
 
contains
 
subroutine print_sorted_by_length(list)
character(len=*) :: list(:)
integer :: i
 
list(sort_int(len_trim(list)))=list ! sort by length from small to large
write(*,'(i9,1x,a)')(len_trim(list(i)), list(i),i=size(list),1,-1)! print from last to first
write(*,*)
 
end subroutine print_sorted_by_length
 
function sort_int(input) result(counts) ! **very** inefficient mini index sort
integer :: input(:), counts(size(input)), i
counts=[(count(input(i) > input)+count(input(i) == input(:i)),i=1, size(input) )]
end function sort_int
 
end program demo_sort_indexed
</syntaxhighlight>
{{out}}
<pre>
7 shorter
6 longer
 
9 123456789
7 1234567
6 abcdef
4 abcd
 
5 jumps
5 brown
5 quick
4 lazy
4 over
3 dog
3 the
3 fox
3 the
 
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">local fn MyArraySortFunction( obj1 as CFTypeRef, obj2 as CFTypeRef, context as ptr ) as NSComparisonResult
NSComparisonResult result = NSOrderedDescending
if len(obj1) >= len(obj2) then result = NSOrderedAscending
end fn = result
 
void local fn DoIt
CFStringRef string1 = @"abcd", string2 = @"abcdef", s
 
if len(string1) >= len(string2)
print string1,len(string1)
print string2,len(string2)
else
print string2,len(string2)
print string1,len(string1)
end if
 
print
text ,,,,, 85
 
CFArrayRef strings = @[@"abcd",@"123456789",@"abcdef",@"1234567"]
strings = fn ArraySortedArrayUsingFunction( strings, @fn MyArraySortFunction, NULL )
for s in strings
print s,len(s)
next
end fn
 
window 1
 
fn DoIt
 
HandleEvents</syntaxhighlight>
 
Output:
<pre>abcdef 6
abcd 4
 
123456789 9
1234567 7
abcdef 6
abcd 4</pre>
=={{header|Go}}==
{{works with|Go|1.8+}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"os"
"sort"
)
 
func main() {
// If no command-line arguments are specified when running the program, use example data
if len(os.Args) == 1 {
compareStrings("abcd", "123456789", "abcdef", "1234567")
} else {
// First argument, os.Args[0], is program name. Command-line arguments start from 1
strings := os.Args[1:]
compareStrings(strings...)
}
}
 
// Variadic function that takes any number of string arguments for comparison
func compareStrings(strings ...string) {
// "strings" slice is sorted in place
// sort.SliceStable keeps strings in their original order when elements are of equal length (unlike sort.Slice)
sort.SliceStable(strings, func(i, j int) bool {
return len(strings[i]) > len(strings[j])
})
 
for _, s := range strings {
fmt.Printf("%d: %s\n", len(s), s)
}
}</syntaxhighlight>
sort.SliceStable takes comparison function "less" as a second argument. As long as the function satisfies Interface type's Less method you can use it for comparison, see [https://pkg.go.dev/sort?utm_source=gopls#SliceStable SliceStable]
<syntaxhighlight lang="go">comparisonFunction := func(i, j int) bool {
return len(strings[i]) > len(strings[j])
}
 
sort.SliceStable(strings, comparisonFunction)</syntaxhighlight>
{{output}}
.\main.exe
<pre>9: 123456789
7: 1234567
6: abcdef
4: abcd</pre>
.\main.exe The quick brown fox jumps over the lazy dog
<pre>5: quick
5: brown
5: jumps
4: over
4: lazy
3: The
3: fox
3: the
3: dog</pre>
=={{header|Harbour}}==
We can, easily, realize this task with Harbour, utilizing its strong array-handling set of functions.
<syntaxhighlight lang="visualfoxpro">
 
PROCEDURE Main()
LOCAL s1 := "The long string"
LOCAL s2 := "The short string"
LOCAL a := { s1, s2 }
LOCAL s3
 
? s3 := "Here is how you can print the longer string first using Harbour language"
?
? "-------------------------------------------"
PrintTheLongerFirst( a )
a := hb_ATokens( s3, " " )
? "-------------------------------------------"
PrintTheLongerFirst( a )
? "-------------------------------------------"
RETURN
 
FUNCTION PrintTheLongerFirst( a )
LOCAL n, tmp
a := ASort( a,,, {|x,y| Len(x) > Len(y) } )
n:= Len( a[1] )
AEval( a, { |e| tmp := n-Len(e), Qout( e, Space(tmp) + ;
hb_strFormat( "(length = %d chars)", Len(e) ) ) } )
RETURN NIL
</syntaxhighlight>
Output:
<pre>
Here is how you can print the longer string first using Harbour language
-------------------------------------------
The short string (length = 16 chars)
The long string (length = 15 chars)
-------------------------------------------
language (length = 8 chars)
Harbour (length = 7 chars)
longer (length = 6 chars)
string (length = 6 chars)
print (length = 5 chars)
first (length = 5 chars)
using (length = 5 chars)
Here (length = 4 chars)
how (length = 3 chars)
you (length = 3 chars)
can (length = 3 chars)
the (length = 3 chars)
is (length = 2 chars)
-------------------------------------------
</pre>
=={{header|Haskell}}==
Using native String type:
<langsyntaxhighlight lang="haskell">task s1 s2 = do
let strs = if length s1 > length s2 then [s1, s2] else [s2, s1]
mapM_ (\s -> putStrLn $ show (length s) ++ "\t" ++ show s) strs</langsyntaxhighlight>
 
<pre>λ> task "short string" "longer string"
Line 69 ⟶ 1,565:
 
or more practically useful Text:
<langsyntaxhighlight lang="haskell">import qualified Data.Text as T
 
taskT s1 s2 = do
let strs = if T.length s1 > T.length s2 then [s1, s2] else [s2, s1]
mapM_ (\s -> putStrLn $ show (T.length s) ++ "\t" ++ show s) strs</langsyntaxhighlight>
 
<pre>λ> :set -XOverloadedStrings
Line 79 ⟶ 1,575:
13 "longer string"
12 "short string"</pre>
 
=={{header|J}}==
<syntaxhighlight lang=J>
NB. solution
chars=: 9&u:@>
longestFirst=: \: #@chars
lengthAndString=: ([:":@,.#@chars),.' ',.chars
 
NB. `Haruno-umi Hinemosu-Notari Notarikana'
NB. Spring ocean ; Swaying gently ; All day long.
 
lengthAndString longestFirst '春の海';'ひねもすのたり';'のたりかな'
7 ひねもすのたり
5 のたりかな
3 春の海
lengthAndString longestFirst '1234567';'abcd';'123456789';'abcdef'
9 123456789
7 1234567
6 abcdef
4 abcd
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
{{Works with|Java 11, Java 17}}
import java.util.ArrayList;
<lang Java>package stringlensort;
import java.util.Comparator;
import java.util.List;
</syntaxhighlight>
<syntaxhighlight lang="java">
void printCompare(String stringA, String stringB) {
if (stringA.length() > stringB.length()) {
System.out.printf("%d %s%n", stringA.length(), stringA);
System.out.printf("%d %s%n", stringB.length(), stringB);
} else {
System.out.printf("%d %s%n", stringB.length(), stringB);
System.out.printf("%d %s%n", stringA.length(), stringA);
}
}
 
void printDescending(String... strings) {
List<String> list = new ArrayList<>(List.of(strings));
list.sort(Comparator.comparingInt(String::length).reversed());
for (String string : list)
System.out.printf("%d %s%n", string.length(), string);
}
</syntaxhighlight>
<pre>
4 abcd
3 abc
</pre>
<pre>
9 123456789
7 1234567
6 abcdef
4 abcd
</pre>
 
<br />
An alternate demonstration
{{Works with| Java | 11 }}
{{Works with| Java | 17 }}
<syntaxhighlight lang="java">package stringlensort;
 
import java.io.PrintStream;
Line 135 ⟶ 1,689:
}
}
}</langsyntaxhighlight>
 
{{output}}
Line 143 ⟶ 1,697:
"abcd" has length 4 and is the shortest string</pre>
 
=={{header|jqJavaScript}}==
JavaScript (ECMA Script) file stringlensort.js.
{{works with|jq}}
<syntaxhighlight lang="javascript">/**
'''Works with gojq, the Go implementation of jq'''
* Compare and report strings lengths.
<lang jq>
*
def s1: "longer";
* @param {Element} input - a TextArea DOM element with input
def s2: "shorter😀";
* @param {Element} output - a TextArea DOM element for output
*/
function compareStringsLength(input, output) {
 
// Safe defaults.
[s1,s2]
//
| sort_by(length)
output.value = "";
| reverse[]
let output_lines = [];
| "\"\(.)\" has length (codepoints) \(length) and utf8 byte length \(utf8bytelength)."
 
// Split input into an array of lines.
</lang>
//
{{out}}
let strings = input.value.split(/\r\n|\r|\n/g);
<pre>
 
"shorter😀" has length (codepoints) 8 and utf8 byte length 11.
// Is strings array empty?
"longer" has length (codepoints) 6 and utf8 byte length 6.
//
</pre>
if (strings && strings.length > 0) {
 
// Remove leading and trailing spaces.
//
for (let i = 0; i < strings.length; i++)
strings[i] = strings[i].trim();
 
// Sort by lengths.
//
strings.sort((a, b) => a.length - b.length);
 
// Remove empty strings.
//
while (strings[0] == "")
strings.shift();
 
// Check if any strings remain.
//
if (strings && strings.length > 0) {
 
// Get min and max length of strings.
//
const min = strings[0].length;
const max = strings[strings.length - 1].length;
 
// Build output verses - longest strings first.
//
for (let i = strings.length - 1; i >= 0; i--) {
let length = strings[i].length;
let predicate;
if (length == max) {
predicate = "is the longest string";
} else if (length == min) {
predicate = "is the shortest string";
} else {
predicate = "is neither the longest nor the shortest string";
}
output_lines.push(`"${strings[i]}" has length ${length} and ${predicate}\n`);
}
 
// Send all lines from output_lines array to an TextArea control.
//
output.value = output_lines.join('');
}
}
}
 
document.getElementById("input").value = "abcd\n123456789\nabcdef\n1234567";
compareStringsLength(input, output);</syntaxhighlight>
HTML file (with embeded CSS) to run the script.
<syntaxhighlight lang="html"><html>
 
<head>
<style>
div {
margin-top: 4ch;
margin-bottom: 4ch;
}
 
label {
display: block;
margin-bottom: 1ch;
}
 
textarea {
display: block;
}
 
input {
display: block;
margin-top: 4ch;
margin-bottom: 4ch;
}
</style>
</head>
 
<body>
<main>
<form>
<div>
<label for="input">Input:
</label>
<textarea rows="20" cols="80" id="input"></textarea>
</div>
<input type="button" value="press to compare strings" onclick="compareStringsLength(input, output);">
</input>
<div>
<label for="output">Output:
</label>
<textarea rows="20" cols="80" id="output"></textarea>
</div>
</form>
</main>
<script src="stringlensort.js"></script>
</body>
 
</html></syntaxhighlight>
{{Output}}
<pre>"123456789" has length 9 and is the longest string
"1234567" has length 7 and is neither the longest nor the shortest string
"abcdef" has length 6 and is neither the longest nor the shortest string
"abcd" has length 4 and is the shortest string</pre>
 
=={{header|Julia}}==
Line 169 ⟶ 1,828:
The Char data type in Julia is a 32-bit, potentially Unicode data type, so that if we enumerate a String
as a Char array, we get a series of 32-bit characters:
<langsyntaxhighlight lang="julia">s = "niño"
println("Position Char Bytes\n==============================")
for (i, c) in enumerate(s)
println("$i $c $(sizeof(c))")
end
</langsyntaxhighlight>{{out}}
<pre>
Position Char Bytes
Line 189 ⟶ 1,848:
one byte, an error is thrown for bad indexing. This can be demonstrated by casting
the above string to codeunits:
<langsyntaxhighlight lang="julia">println("Position Codeunit Bytes\n==============================")
for (i, c) in enumerate(codeunits(s))
println("$i $(string(c, base=16)) $(sizeof(c))")
end
</langsyntaxhighlight>{{out}}
<pre>
Position Codeunit Bytes
Line 207 ⟶ 1,866:
of "niño" as codeunits (ie, 8 bit bytes) is 5. Indexing into the 4th position
results in an error:
<langsyntaxhighlight lang="julia">
julia> s[4]
ERROR: StringIndexError: invalid index [4], valid nearby indices [3]=>'ñ', [5]=>'o'
</syntaxhighlight>
</lang>
 
So, whether a string is longer or shorter depends on the encoding, as below:
<langsyntaxhighlight lang="julia">length("ñññ") < length("nnnn") # true, and the usual meaning of length of a String
 
length(codeunits("ñññ")) > length(codeunits("nnnn")) # true as well
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
def s1: "longer";
def s2: "shorter😀";
 
[s1,s2]
| sort_by(length)
| reverse[]
| "\"\(.)\" has length (codepoints) \(length) and utf8 byte length \(utf8bytelength)."
 
</syntaxhighlight>
{{out}}
<pre>
"shorter😀" has length (codepoints) 8 and utf8 byte length 11.
"longer" has length (codepoints) 6 and utf8 byte length 6.
</pre>
 
=={{header|Lambdatalk}}==
Lambdatalk comes with primitives working on words, [W.equal?, W.length, ...], on sentences, [S.empty?, S.first, S.rest, ...] and pairs, [P.new, P.left, P.right, ...].
 
Using these primitives we define 2 helper functions, [L.new, L.disp], to build and display a list according to a chosen format.
<syntaxhighlight lang="scheme">
{def L.new
{lambda {:s}
{if {S.empty? {S.rest :s}}
then {P.new {S.first :s} nil}
else {P.new {S.first :s} {L.new {S.rest :s}}}}}}
-> L.new
 
{def L.disp
{lambda {:l}
{if {W.equal? :l nil}
then
else {br} {W.length {P.left :l}} : {P.left :l}
{L.disp {P.right :l}}}}}
-> L.disp
</syntaxhighlight>
 
For instance
 
<syntaxhighlight lang="scheme">
{def B {L.new abcd 123456789 abcdef 1234567}}
-> B
 
{L.disp {B}}
->
4 : abcd
9 : 123456789
6 : abcdef
7 : 1234567
</syntaxhighlight>
 
Then we define the L.sort function waiting for a predicate function and a list.
 
<syntaxhighlight lang="scheme">
{def L.sort
{lambda {:filter :l}
{if {W.equal? :l nil}
then nil
else {L.insert {P.left :l} :filter
{L.sort :filter {P.right :l}}}}}}
-> L.sort
 
{def L.insert
{lambda {:x :filter :l}
{if {W.equal? :l nil}
then {P.new :x nil}
else {if {:filter :x {P.left :l}}
then {P.new :x :l}
else {P.new {P.left :l}
{L.insert :x :filter {P.right :l}}}}}}}
-> L.insert
</syntaxhighlight>
 
Using the following predicate function (which could be anonymous) testing the length of 2 words
 
<syntaxhighlight lang="scheme">
{def filter
{lambda {:a :b}
{> {W.length :a} {W.length :b}}}}
-> filter
</syntaxhighlight>
 
we display the B list sorted according to the length of its elements.
 
<syntaxhighlight lang="scheme">
{L.disp {L.sort filter {B}}}
->
9 : 123456789
7 : 1234567
6 : abcdef
4 : abcd
</syntaxhighlight>
 
Note that in lambdatalk words (and numbers) don't need to be quoted.
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function test(list)
table.sort(list, function(a,b) return #a > #b end)
for _,s in ipairs(list) do print(#s, s) end
end
test{"abcd", "123456789", "abcdef", "1234567"}</syntaxhighlight>
{{out}}
<pre>9 123456789
7 1234567
6 abcdef
4 abcd</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">list = {"abcd", "123456789", "abcdef", "1234567"};
Reverse@SortBy[list, StringLength] // TableForm</syntaxhighlight>
 
{{out}}<pre>
123456789
1234567
abcdef
abcd
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
// Simple version
print "Simple version:"
s2 = "This is the first string."
s1 = "This is string number two."
 
if s1.len > s2.len then
print s1.len + ": " + s1
print s2.len + ": " + s2
else
print s2.len + ": " + s2
print s1.len + ": " + s1
end if
 
// Extra credit. More than 2 strings
strings = ["qwerty", "abc", "#FFFFFFFF", "255,255,255,255", "3.14159"]
pairs = []
for string in strings
pairs.push([string, string.len])
end for
// sort by index descending
pairs.sort(1, false)
print
print "Extra credit:"
for pair in pairs
print pair[1] + ": " + pair[0]
end for
</syntaxhighlight>
 
{{out}}
<pre>Simple version:
26: This is string number two.
25: This is the first string.
 
Extra credit:
15: 255,255,255,255
9: #FFFFFFFF
7: 3.14159
6: qwerty
3: abc
</pre>
 
=={{header|Nim}}==
Line 224 ⟶ 2,047:
If we want to manage a string as a Unicode sequence of code points, we have to use the module <code>unicode</code>. We can convert a string in a sequence of runes, each rune being a unicode UTF-32 value. The length of this sequence is the number of code points.
 
<langsyntaxhighlight Nimlang="nim">import strformat, unicode
 
const
Line 231 ⟶ 2,054:
 
echo &"“{S2}”, byte length = {S2.len}, code points: {S2.toRunes.len}"
echo &"“{S1}”, byte length = {S1.len}, code points: {S1.toRunes.len}"</langsyntaxhighlight>
 
{{out}}
<pre>“marché”, byte length = 7, code points: 6
“marche”, byte length = 6, code points: 6</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let () =
["abcd"; "123456789"; "abcdef"; "1234567"]
|> List.rev_map (fun s -> String.length s, s)
|> List.sort (Fun.flip compare)
|> List.iter (fun (l, s) -> Printf.printf "%u %s\n" l s)</syntaxhighlight>
{{out}}
<pre>
9 123456789
7 1234567
6 abcdef
4 abcd
</pre>
 
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<langsyntaxhighlight lang="pascal">program compareLengthOfStrings(output);
 
const
Line 301 ⟶ 2,138:
analyzeLengths;
printSortedByLengths
end.</langsyntaxhighlight>
{{out}}
<pre> 11 RosettaCode
Line 307 ⟶ 2,144:
6 Pascal
6 Foobar</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Compare_length_of_two_strings
use warnings;
 
for ( 'shorter thelonger', 'abcd 123456789 abcdef 1234567' )
{
print "\nfor strings => $_\n";
printf "length %d: %s\n", length(), $_
for sort { length $b <=> length $a } split;
}</syntaxhighlight>
{{out}}
<pre>
 
for strings => shorter thelonger
length 9: thelonger
length 7: shorter
 
for strings => abcd 123456789 abcdef 1234567
length 9: 123456789
length 7: 1234567
length 6: abcdef
length 4: abcd
</pre>
 
=={{header|Phix}}==
Lengths are in bytes, for codepoints use length(utf8_to_utf32()) or similar.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"abcd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"123456789"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"abcdef"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1234567"</span><span style="color: #0000FF;">},</span>
Line 316 ⟶ 2,179:
<span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lens</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lens</span><span style="color: #0000FF;">))))</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">printf</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"%s (length %d)\n"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lens</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">)})})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 324 ⟶ 2,187:
abcd (length 4)
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Compare_length_of_two_strings
by Galileo, 10/2022 #/
 
include ..\Utilitys.pmt
 
def getlen len swap 2 tolist enddef
 
( "abcd" "123456789" "abcdef" "1234567" ) getid getlen map
 
sort reverse lprint</syntaxhighlight>
{{out}}
<pre>[9, "123456789"][7, "1234567"][6, "abcdef"][4, "abcd"]
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
<?php
 
 
function retrieveStrings()
{
if (isset($_POST['input'])) {
$strings = explode("\n", $_POST['input']);
} else {
$strings = ['abcd', '123456789', 'abcdef', '1234567'];
}
return $strings;
}
 
 
function setInput()
{
echo join("\n", retrieveStrings());
}
 
 
function setOutput()
{
$strings = retrieveStrings();
 
// Remove empty strings
//
$strings = array_map('trim', $strings);
$strings = array_filter($strings);
 
if (!empty($strings)) {
usort($strings, function ($a, $b) {
return strlen($b) - strlen($a);
});
$max_len = strlen($strings[0]);
$min_len = strlen($strings[count($strings) - 1]);
foreach ($strings as $s) {
$length = strlen($s);
if ($length == $max_len) {
$predicate = "is the longest string";
} elseif ($length == $min_len) {
$predicate = "is the shortest string";
} else {
$predicate = "is neither the longest nor the shortest string";
}
echo "$s has length $length and $predicate\n";
}
}
}
 
?>
 
 
<!DOCTYPE html>
<html lang="en">
 
<head>
<style>
div {
margin-top: 4ch;
margin-bottom: 4ch;
}
 
label {
display: block;
margin-bottom: 1ch;
}
 
textarea {
display: block;
}
 
input {
display: block;
margin-top: 4ch;
margin-bottom: 4ch;
}
</style>
</head>
 
 
<body>
<main>
<form action=<?php echo $_SERVER['SCRIPT_NAME'] ?> method="post" accept-charset="utf-8">
<div>
<label for="input">Input:
</label>
<textarea rows='20' cols='80' name='input'><?php setInput(); ?></textarea>
</label>
</div>
<input type="submit" value="press to compare strings">
</input>
<div>
<label for="Output">Output:
</label>
<textarea rows='20' cols='80' name='output'><?php setOutput(); ?></textarea>
</div>
</form>
</main>
</body>
 
</html></syntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
declare (S1, S2) character (20) varying; /* 11 Aug 2022 */
get (S1, S2);
if length(S1) > length(S2) then
put (length(S1), S1);
else
put (length(S2), S2);
</syntaxhighlight>
 
=={{header|Python}}==
===Naive solution===
{{works with | Python | 3.8}}
<langsyntaxhighlight Pythonlang="python">A = 'I am string'
B = 'I am string too'
 
Line 339 ⟶ 2,331:
else:
print('"' + A + '"', 'has length', len(A), 'and it is as long as the second string')
print('"' + B + '"', 'has length', len(B), 'and it is as long as the second string')</langsyntaxhighlight>
{{output}}
<pre>
Line 347 ⟶ 2,339:
 
===Advanced solution===
{{works with | Python | 3.8}}
 
<lang Python>"""
The solution below has some imperfection. When the longest strings of characters are of equal length, instead of describing them as "''one of the longest''" they are described as "''the longest''". This works similarly for the shortest strings. Also, if all strings (in this case where there is only one) have the same length it is not printed that they are the shortest strings. Of course, this could be improved.
 
<syntaxhighlight lang="python">"""
An example code for the task "Compare length of two strings" (Rosseta Code).
 
Line 367 ⟶ 2,362:
lengths and classification as the longest, shortest or average length.
 
Note that for N > 0 such objects (e.g., strings, byte stringsbytes, lists) it is
possible that exactly M > 0 of them will be of the maximum length, K > 0 of
them will be of the minimum length. In particular, it is possible that all
objects will be exactly the same length. So we assume that if an object has
Line 391 ⟶ 2,386:
lengths_and_objects = zip(lengths, objects)
 
# Longer phrases make translationtranslations into other natural languages easier.
#
has_length = _('has length')
if all(isinstance(obj, str) for obj in objects):
predicate_max = _('and is the longest string')
predicate_min = _('and is the shortest string')
predicate_ave = _('and is neither the longest nor the shortest string')
else:
predicate_max = _('and is the longest object')
predicate_min = _('and is the shortest object')
predicate_ave = _('and is neither the longest nor the shortest object')
 
if sorted_:
Line 412 ⟶ 2,408:
else:
predicate = predicate_ave
print(f'"{obj}", hashas_length, length, {length} and {predicate}')
 
 
Line 428 ⟶ 2,424:
print()
compare_and_report_length(*LIST)
print()</lang>
</syntaxhighlight>
 
{{out}}
Line 443 ⟶ 2,440:
"abcdef" has length 6 and is neither the longest nor the shortest string
"abcd" has length 4 and is the shortest string
</pre>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
Dim Words(1 To 4) As String
Dim Lengths As Integer, Index As Integer, Position As Integer, Done As String, Index2 As Integer
' inititialization
Words(1) = "abcd"
Words(2) = "123456789"
Words(3) = "abcdef"
Words(4) = "1234567"
 
Print " Word Length"
For Index2 = 1 To 4 Step 1
Lengths = 0
Position = 0
For Index = 1 To 4 Step 1
If Lengths < Len(Words(Index)) And InStr(Done, Words(Index) + " ") = 0 Then
Lengths = Len(Words(Index))
Position = Index
End If
Next Index
Done = Done + Words(Position) + " /@/"
Print Words(Position), Len(Words(Position))
Next Index2
</syntaxhighlight>
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> $ "A short string of"
$ "A slightly longer string of"
2dup size dip size > if swap
dup echo$ sp size echo say " characters." cr
dup echo$ sp size echo say " characters." cr cr
 
' [ $ "From troubles of the world I turn to ducks,"
$ "Beautiful comical things"
$ "Sleeping or curled"
$ "Their heads beneath white wings"
$ "By water cool,"
$ "Or finding curious things"
$ "To eat in various mucks"
$ "Beneath the pool," ]
[] swap witheach [ do nested join ]
sortwith [ size dip size < ]
witheach [ echo$ cr ] </syntaxhighlight>
 
{{out}}
 
<pre>A slightly longer string of 27 characters.
A short string of 17 characters.
 
From troubles of the world I turn to ducks,
Their heads beneath white wings
Or finding curious things
Beautiful comical things
To eat in various mucks
Sleeping or curled
Beneath the pool,
By water cool,
</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
 
(define strings '("abcd" "123456789" "abcdef" "1234567"))
 
(for ([i (sort strings > #:key string-length)])
(printf "'~a' is length ~a~n" i (string-length i)))
</syntaxhighlight>
{{out}}
<pre>
'123456789' is length 9
'1234567' is length 7
'abcdef' is length 6
'abcd' is length 4
</pre>
 
Line 450 ⟶ 2,525:
In the modern world, string "length" is pretty much a useless measurement, especially in the absence of a specified encoding; hence Raku not even having an operator: "length" for strings.
 
<syntaxhighlight lang="raku" perl6line>say 'Strings (👨‍👩‍👧‍👦, 🤔🇺🇸, BOGUS!) sorted: "longest" first:';
say "$_: characters:{.chars}, Unicode code points:{.codes}, UTF-8 bytes:{.encode('UTF8').bytes}, UTF-16 bytes:{.encode('UTF16').bytes}" for <👨‍👩‍👧‍👦 BOGUS! 🤔🇺🇸>.sort: -*.chars;</langsyntaxhighlight>
{{out}}
<pre>Strings (👨‍👩‍👧‍👦, 🤔🇺🇸, BOGUS!) sorted: "longest" first:
Line 458 ⟶ 2,533:
👨‍👩‍👧‍👦: characters:1, Unicode code points:7, UTF-8 bytes:25, UTF-16 bytes:22</pre>
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/* REXX */
list = '"abcd","123456789","abcdef","1234567"'
Do i=1 By 1 While list>''
Parse Var list s.i ',' list
s.i=strip(s.i,,'"')
End
n=i-1
Do While n>1
max=0
Do i=1 To n
If length(s.i)>max Then Do
k=i
max=length(s.i)
End
End
Call o s.k
If k<n Then
s.k=s.n
n=n-1
End
Call o s.1
Exit
o:
Say length(arg(1)) arg(1)
Return</syntaxhighlight>
{{out}}
<pre>9 123456789
7 1234567
6 abcdef
4 abcd</pre>
=={{header|Ring}}==
===Two strings===
<langsyntaxhighlight lang="ring">
see "working..." + nl
 
Line 475 ⟶ 2,582:
see "" + first + " len = " + len(first) + nl + second + " len = " + len(second) + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 485 ⟶ 2,592:
</pre>
===More than two strings===
<langsyntaxhighlight lang="ring">
see "working..." + nl
 
Line 503 ⟶ 2,610:
next
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 514 ⟶ 2,621:
done...
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ " [" OVER SIZE →STR "]" + + +
≫ ''''FormatString'''' STO
'''IF''' OVER SIZE OVER SIZE > '''THEN''' SWAP '''END'''
'''FormatString''' SWAP '''FormatString'''
≫ ''''SRT2S'''' STO
LIST→ → len
≪ len 1 '''FOR''' n
1 n 1 - '''START'''
'''IF''' OVER SIZE OVER SIZE <
'''THEN''' SWAP '''END'''
n ROLLD
'''NEXT'''
n ROLLD
-1 '''STEP'''
1 len '''START''' len ROLL '''FormatString''' '''NEXT'''
≫ ≫ ''''SRTLS'''' STO
|
''( "string" -- "string [length]" )''
''( "s1" "s2" -- "s1 [l1]" "s2 [l2]" )''
Swap strings if necessary
Format strings
''( { strings } -- { strings } )''
Push list in the stack
Use selection sort algorithm (favouring code size to execution time)
Format each string in the stack
|}
The following lines of code deliver what is required:
"AB" "ABCD" '''STR2S'''
{ "ABC" "AB" "ABCD" } '''SRTLS'''
{{out}}
<pre>
5: "ABCD [4]"
4: "AB [2]"
3: "ABCD [4]"
2: "ABC [3]"
1: "AB [2]"
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
a, b = "Given two strings", "of different length"
[a,b].sort_by{|s| - s.size }.each{|s| puts s + " (size: #{s.size})"}
 
list = ["abcd","123456789","abcdef","1234567"]
puts list.sort_by{|s|- s.size}
</syntaxhighlight>
{{out}}
<pre>of different length (size: 19)
Given two strings (size: 17)
123456789
1234567
abcdef
abcd</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn compare_and_report<T: ToString>(string1: T, string2: T) -> String {
let strings = [string1.to_string(), string2.to_string()];
let difference = strings[0].len() as i32 - strings[1].len() as i32;
if difference == 0 { // equal
format!("\"{}\" and \"{}\" are of equal length, {}", strings[0], strings[1], strings[0].len())
} else if difference > 1 { // string1 > string2
format!("\"{}\" has length {} and is the longest\n\"{}\" has length {} and is the shortest", strings[0], strings[0].len(), strings[1], strings[1].len())
} else { // string2 > string1
format!("\"{}\" has length {} and is the longest\n\"{}\" has length {} and is the shortest", strings[1], strings[1].len(), strings[0], strings[0].len())
}
}
 
fn main() {
println!("{}", compare_and_report("a", "b"));
println!("\n{}", compare_and_report("cd", "e"));
println!("\n{}", compare_and_report("f", "gh"));
}
</syntaxhighlight>
 
{{out}}
<pre>
"a" and "a" are of equal length, 1
 
"d" has length 1 and is the longest
"bc" has length 2 and is the shortest
 
"fg" has length 2 and is the longest
"e" has length 1 and is the shortest
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object Example extends App {
val strings = Array("abcd", "123456789", "abcdef", "1234567")
compareAndReportStringsLength(strings)
 
def compareAndReportStringsLength(strings: Array[String]): Unit = {
if (strings.nonEmpty) {
val Q = '"'
val hasLength = " has length "
val predicateMax = " and is the longest string"
val predicateMin = " and is the shortest string"
val predicateAve = " and is neither the longest nor the shortest string"
 
val sortedStrings = strings.sortBy(-_.length)
val maxLength = sortedStrings.head.length
val minLength = sortedStrings.last.length
 
sortedStrings.foreach { str =>
val length = str.length
val predicate = length match {
case `maxLength` => predicateMax
case `minLength` => predicateMin
case _ => predicateAve
}
println(s"$Q$str$Q$hasLength$length$predicate")
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
"123456789" has length 9 and is the longest string
"1234567" has length 7 and is neither the longest nor the shortest string
"abcdef" has length 6 and is neither the longest nor the shortest string
"abcd" has length 4 and is the shortest string
 
</pre>
 
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule: {
v: ["abcd","123456789","abcdef","1234567"],
 
_start: (λ
(for s in (sort v (λ l String() r String()
(ret (< (size r) (size l))))) do
(lout width: 10 s " : " (size s) " code points") )
)
}</syntaxhighlight>
{{out}}
<pre>
123456789 : 9 code points
1234567 : 7 code points
abcdef : 6 code points
abcd : 4 code points
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="go">// Compare lenth of two strings, in V
// Tectonics: v run compare-length-of-two-strings.v
module main
 
// starts here
pub fn main() {
mut strs := ["abcd","123456789"]
println("Given: $strs")
strs.sort_by_len()
for i := strs.len-1; i >= 0; i-- {
println("${strs[i]}: with length ${strs[i].len}")
}
 
// more than 2 strings. note = vs :=, := for definition, = for assignment
strs = ["abcd","123456789","abcdef","1234567"]
println("\nGiven: $strs")
strs.sort_by_len()
for i := strs.len-1; i >= 0; i-- {
println("${strs[i]}: with length ${strs[i].len}")
}
}</syntaxhighlight>
 
{{out}}
<pre>prompt$ v run compare-length-of-two-strings.v
Given: ['abcd', '123456789']
123456789: with length 9
abcd: with length 4
 
Given: ['abcd', '123456789', 'abcdef', '1234567']
123456789: with length 9
1234567: with length 7
abcdef: with length 6
abcd: with length 4</pre>
 
=={{header|Wren}}==
Line 524 ⟶ 2,836:
 
Unicode grapheme clusters, where what appears to be a single 'character' may in fact be an amalgam of several codepoints, are not directly supported by Wren but it is possible to measure the length in grapheme clusters of a string (i.e. the number of ''user perceived characters'') using the ''Graphemes.clusterCount'' method of the Wren-upc module.
<langsyntaxhighlight ecmascriptlang="wren">import "./upc" for Graphemes
 
var printCounts = Fn.new { |s1, s2, c1, c2|
Line 563 ⟶ 2,875:
System.write("Sorting in descending order by length in codepoints:\n%(list) -> ")
list.sort { |a, b| a.count > b.count }
System.print(list)</langsyntaxhighlight>
 
{{out}}
Line 593 ⟶ 2,905:
Sorting in descending order by length in codepoints:
[abcd, 123456789, abcdef, 1234567] -> [123456789, 1234567, abcdef, abcd]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">string 0; \use zero-terminated string convention
 
func StrLen(A); \Return number of characters in an ASCIIZ string
char A;
int I;
for I:= 0 to -1>>1 do
if A(I) = 0 then return I;
 
char List;
int M, N, SN, Len, Max;
[List:= ["abcd","123456789","abcdef","1234567"];
for M:= 0 to 3 do
[Max:= 0;
for N:= 0 to 3 do
[Len:= StrLen(@List(N,0));
if Len > Max then [Max:= Len; SN:= N];
];
Text(0, @List(SN,0));
Text(0, " length is "); IntOut(0, StrLen(@List(SN,0))); CrLf(0);
List(SN, 0):= 0; \truncate largest string
];
]</syntaxhighlight>
 
{{out}}
<pre>
123456789 length is 9
1234567 length is 7
abcdef length is 6
abcd length is 4
</pre>
 
=={{header|Z80 Assembly}}==
<langsyntaxhighlight lang="z80">Terminator equ 0 ;null terminator
PrintChar equ &BB5A ;Amstrad CPC BIOS call, prints accumulator to screen as an ASCII character.
 
Line 719 ⟶ 3,063:
add a,&F0
adc a,&40 ;This sequence converts a 4-bit hex digit to its ASCII equivalent.
jp PrintChar</langsyntaxhighlight>
 
{{out}}
27

edits