Longest string challenge: Difference between revisions
no edit summary
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
No edit summary |
||
Line 968:
</syntaxhighlight>
{{out}}
<pre>
ccc
ddd
ggg
</pre>
=={{header|FutureBasic}}==
'''Option 1 (old school)'''
<syntaxhighlight lang="futurebasic">
local fn FindLongest( test as CFArrayRef ) as CFStringRef
'~'1
CFStringRef s, t1 = @"", t2 = @""
for s in test
if ( len(s) > len(t1) )
t1 = s
t2 = fn StringWithFormat( @"%@\n", s )
else
if ( len(s) == len(t1) )
t2 = fn StringWithFormat( @"%@%@\n", t2, s )
end if
end if
next
end fn = t2
print fn FindLongest( @[@"a", @"bb", @"ccc", @"ddd", @"ee", @"f", @"ggg"] )
HandleEvents
</syntaxhighlight>
'''Option 2 (modern)'''
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
_longestStringsToDisplay = 3
local fn LongestStrings( string as CFStringRef )
CFArrayRef array = fn StringComponentsSeparatedByString( string, @" " )
SortDescriptorRef sortAscending = fn SortDescriptorWithKey( @"length", YES )
array = fn ArraySortedArrayUsingDescriptors( array, @[sortAscending] )
array = fn ArraySubarrayWithRange( array, fn CFRangeMake( len(array)-_longestStringsToDisplay, _longestStringsToDisplay ) )
NSLog( @"%@\n%@\n%@", array[0], array[1], array[2] )
end fn
fn LongestStrings( @"a bb ccc ddd ee f ggg" )
HandleEvents
</syntaxhighlight>
'''Output for either:'''
<pre>
ccc
|