Compare length of two strings: Difference between revisions

m
no edit summary
(Undo revision 357306 by Chkas (talk))
mNo edit summary
 
(10 intermediate revisions by 7 users not shown)
Line 727:
{{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}}===
Line 855 ⟶ 874:
 
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}}===
Line 874 ⟶ 915:
<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}}===
Line 1,053 ⟶ 1,118:
</pre>
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|10+}}
<syntaxhighlight lang="csharp">using System;
<syntaxhighlight lang="csharp">
using System.Collections.Generic;
void WriteSorted(string[] strings)
 
namespace example
{
var sorted = strings.OrderByDescending(x => x.Length);
class Program
foreach(var s in sorted) Console.WriteLine($"{s.Length}: {s}");
{
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);
}
}
}
 
}
}
WriteSorted(new string[] { "abcd", "123456789", "abcdef", "1234567" });
</syntaxhighlight>
 
{{output}}
<pre>
<pre>"123456789" has length 9 and is the longest string
9: 123456789
"1234567" has length 7 and is neither the longest nor the shortest string
7: 1234567
"abcdef" has length 6 and is neither the longest nor the shortest string
6: abcdef
"abcd" has length 4 and is the shortest string
4: abcd
</pre>
 
Line 1,229 ⟶ 1,254:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc stringLengthscmp a$ b$ . .
lineA$if = "\"" &len a$ & "\" (length: " &< len ab$ & ")"
swap a$ b$
lineB$ = "\"" & b$ & "\" (length: " & len b$ & ")"
if len a$ >= len b$
print lineA$ ; print lineB$
else
print lineB$ ; print lineA$
.
print a$ & " - " & len a$
print b$ & " - " & len b$
print ""
.
call stringLengthscmp "Easy" "Language"
call stringLengthscmp "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")
"Language" (length: 8)
"Easy" (length: 4)
"Rosetta" (length: 7)
"Code" (length: 4)
</pre>
 
Line 2,696 ⟶ 2,729:
"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}}==
Line 2,761 ⟶ 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.
<syntaxhighlight lang="ecmascriptwren">import "./upc" for Graphemes
 
var printCounts = Fn.new { |s1, s2, c1, c2|
27

edits