Compare length of two strings: Difference between revisions

No edit summary
Line 675:
</pre>
 
=={{header|Harbour}}==
We can, easily, realize this task with Harbour, utilizing its strong array-handling set of functions.
<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
</lang>
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:
Anonymous user