Compare length of two strings: Difference between revisions

Content added Content deleted
(J: simplify (though the language lesson stuff will remain in wiki history, should that be of interest...))
Line 1,520: Line 1,520:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang=J>
<pre>
NB. solution
NB. solution
chars=: 9&u:@>
longestFirst=: \: #@chars
lengthAndString=: ([:":@,.#@chars),.' ',.chars

NB. `Haruno-umi Hinemosu-Notari Notarikana'
NB. `Haruno-umi Hinemosu-Notari Notarikana'
NB. Spring ocean ; Swaying gently ; All day long.
NB. Spring ocean ; Swaying gently ; All day long.


,/ _2 }.\ ": (;~ #)&> <@(7&u:);._2 '春の海 ひねもすのたり のたりかな '
lengthAndString longestFirst '春の海';'ひねもすのたり';'のたりかな'
7 ひねもすのたり
│3│春の海 │
│7│ひねもすのたり
5 のたりかな
│5│たりかな
3 春
lengthAndString longestFirst '1234567';'abcd';'123456789';'abcdef'
9 123456789

7 1234567
NB. # y is the tally of items (penultimate dimension) in the array y
6 abcdef
# 323 43 5j3
4 abcd
3
</syntaxhighlight>
# 'literal (a string)'
18
/: 'cbad' NB. index ordering vector (grade up)
2 1 0 3
;: 'j tokenize a sentence.'
┌─┬────────┬─┬─────────┐
│j│tokenize│a│sentence.│
└─┴────────┴─┴─────────┘
#S:0 ;: 'j tokenize a sentence.' NB. length of leaves (lowest box level)
1 8 1 9
A=: '1234567 abcd 123456789 abcdef' NB. global assignment

(\: #S:0) ;: A NB. order by grade down
┌─────────┬───────┬──────┬────┐
│123456789│1234567│abcdef│abcd│
└─────────┴───────┴──────┴────┘
(;:'length literal') , ((;~ #)&> \: #S:0) ;: A NB. box incompatible types with header
┌──────┬─────────┐
│length│literal │
├──────┼─────────┤
│9 │123456789│
├──────┼─────────┤
│7 │1234567 │
├──────┼─────────┤
│6 │abcdef │
├──────┼─────────┤
│4 │abcd │
└──────┴─────────┘

(;:'len vector') , ((;~ #)&> \: #S:0) 0 1 2 3 ; 0 1 ; (i. 8) ; 0
┌───┬───────────────┐
│len│vector │
├───┼───────────────┤
│8 │0 1 2 3 4 5 6 7│
├───┼───────────────┤
│4 │0 1 2 3 │
├───┼───────────────┤
│2 │0 1 │
├───┼───────────────┤
│1 │0 │
└───┴───────────────┘
</pre>


=={{header|Java}}==
=={{header|Java}}==