Compare length of two strings: Difference between revisions

Add Kotlin
(Add Kotlin)
 
(6 intermediate revisions by 4 users not shown)
Line 1,118:
</pre>
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|10+}}
<syntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
 
namespace example
{
class Program
{
static void Main(string[] args)
{
var strings = new string[] { "abcd", "123456789", "abcdef", "1234567" };
compareAndReportStringsLength(strings);
}
 
private static void compareAndReportStringsLength(string[] strings)
{
if (strings.Length > 0)
{
char Q = '"';
string hasLength = " has length ";
string predicateMax = " and is the longest string";
string predicateMin = " and is the shortest string";
string predicateAve = " and is neither the longest nor the shortest string";
string predicate;
 
(int, int)[] li = new (int, int)[strings.Length];
for (int i = 0; i < strings.Length; i++)
li[i] = (strings[i].Length, i);
Array.Sort(li, ((int, int) a, (int, int) b) => b.Item1 - a.Item1);
int maxLength = li[0].Item1;
int minLength = li[strings.Length - 1].Item1;
 
for (int i = 0; i < strings.Length; i++)
{
int length = li[i].Item1;
string str = strings[li[i].Item2];
if (length == maxLength)
predicate = predicateMax;
else if (length == minLength)
predicate = predicateMin;
else
predicate = predicateAve;
Console.WriteLine(Q + str + Q + hasLength + length + predicate);
}
}
}
}
}
</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>
 
===Simple Modern Version===
{{works with|C sharp|C#|12+}}
<syntaxhighlight lang="csharp">
void WriteSorted(string[] strings)
Line 1,324 ⟶ 1,265:
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}}==
Line 1,937 ⟶ 1,894:
"shorter😀" has length (codepoints) 8 and utf8 byte length 11.
"longer" has length (codepoints) 6 and utf8 byte length 6.
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">
fun main() {
printTwoStrings("a short string", "a fairly long string")
printStringsInDescendingLengthOrder(listOf("abcd", "123456789", "abcdef", "1234567"))
}
 
fun printTwoStrings(a: String, b: String) {
val (shorter, longer) = if (a.length < b.length) Pair(a, b) else Pair(b, a)
println("%3d: %s".format(longer.length, longer))
println("%3d: %s".format(shorter.length, shorter))
}
 
fun printStringsInDescendingLengthOrder(strings: Collection<String>) {
strings.sortedByDescending(String::length).forEach {
println("%3d: %s".format(it.length, it))
}
}
</syntaxhighlight>
{{out}}
<pre>
20: a fairly long string
14: a short string
9: 123456789
7: 1234567
6: abcdef
4: abcd
</pre>
 
Line 2,187 ⟶ 2,173:
6 Pascal
6 Foobar</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="pascal">
begin
var (s1,s2) := ('Bye','Hello');
if s1.Length < s2.Length then
Swap(s1,s2);
Println(s1,s1.Length);
Println(s2,s2.Length);
Println;
var strArr := |'wolf','cat','crocodile','tiger'|;
strArr.OrderByDescending(s -> s.Length).PrintLines(s -> $'{s,9} - {s.Length}');
end.
</syntaxhighlight>
{{out}}
<pre>
Hello 5
Bye 3
 
crocodile - 9
tiger - 5
wolf - 4
cat - 3
</pre>
 
=={{header|Perl}}==
Line 2,814 ⟶ 2,824:
</pre>
 
=={{header|Swift}}==
Swift provides a simple ''count'' property to find how many syntactic characters are in any given String. When counting bytes Swift supports Unicode codepoints.
 
In this example we use the ''sorted'' and ''forEach'' methods from the Sequence protocol to first sort our list into a new Array and then print each item in order. String interpolation is used to print the details of each item.
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
Here we use anonymous argument names with the ''sorted'' closure and a named argument with the ''forEach'' to illustrate how to use either style.
MainModule: {
<syntaxhighlight lang="Swift">
v: ["abcd","123456789","abcdef","1234567"],
let list = ["abcd", "abcd🤦‍♂️", "123456789", "abcdef", "1234567"]
 
list.sorted { $0.count > $1.count }.forEach { string in
_start: (λ
print(for s in "\(sortstring) vhas \(λ l String(string.count) r String(characters")
}
(ret (< (size r) (size l))))) do
</syntaxhighlight>
(lout width: 10 s " : " (size s) " code points") )
)
}</syntaxhighlight>
{{out}}
<pre>
123456789 :has 9 code pointscharacters
1234567 :has 7 code pointscharacters
abcdef :has 6 code pointscharacters
abcd🤦‍♂️ has 5 characters
abcd : 4 code points
abcd has 4 characters
</pre>
 
49

edits