Odd word problem: Difference between revisions

m
(Reverse String function refactored for efficiency)
 
(8 intermediate revisions by 5 users not shown)
Line 675:
readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
global inpi inp$ .
func$ read .
inpi += 1
return substr inp$ inpi 1
.
func ispunct c$ .
if c$ = "." or c$ = ":" or c$ = ";" or c$ = ","
return 1
.
return 0
.
func$ handle odd .
c$ = read
if ispunct c$ = 1
return c$
.
if odd = 0
write c$
r$ = handle 0
return r$
else
r$ = handle 1
write c$
return r$
.
.
proc go . .
repeat
c$ = handle odd
write c$
until c$ = "."
odd = 1 - odd
.
print ""
.
repeat
inp$ = input
until inp$ = ""
inpi = 0
go
.
input_data
we,are;not,in,kansas;any,more.
what,is,the;meaning,of:life.
 
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,011 ⟶ 1,060:
<syntaxhighlight lang="futurebasic">
begin globals
short ndx : bool odd : cfstringref stream
NSInteger n1 = 1
end globals
 
local fn recursion
local fn ReverseString( string as CFStringRef ) as CFStringRef
cfstringref ch = mid( stream, ndx, 1 )
NSInteger i, length = len(string)
if fn StringContainsString( @",;:. ", ch ) == no
CFMutableStringRef mutStr = fn MutableStringWithString( string )
ndx++
if odd then fn recursion : print ch; ¬
else print ch; : fn recursion
end if
end fn
 
local fn oddWordTask( s as cfstringref )
for i = 0 to length - 1
ndx = 0 : odd = no : stream = s
CFStringRef chr1 = mid( string, i, 1 )
print : print, stream : print,
CFStringRef chr2 = mid( string, length-i-1, 1 )
while ndx < len( stream )
mid( mutStr, i, 1 ) = chr2
fn recursion : print mid( stream, ndx, 1 );
mid( mutStr, length-i-1, 1 ) = chr1
odd = yes - odd : ndx++
next
end fn = mutStr
 
local fn OddWordTask( string as CFStringRef ) as CFStringRef
CFStringRef tempStr
NSInteger c = 1, n2
CFMutableStringRef mutStr = fn MutableStringNew
MutableStringAppendFormat( mutStr, @"Original text: %@\n", string )
MutableStringAppendFormat( mutStr, @"OddWord parse: \b" )
while (1)
n2 = instr( 0, string, @"[,;:.]", NSRegularExpressionSearch )
tempStr = left( string, n2 )
if ( c mod 2 == 0 )
tempStr = fn StringWithFormat( @"%@%@\b", fn ReverseString( tempStr ), mid( string, n2, 1 ) )
else
tempStr = fn StringWithFormat( @"%@%@\b", tempStr, mid( string, n2, 1 ) )
end if
MutableStringAppendString( mutStr, tempStr )
if fn StringIsEqual( mid( string, n2, 1 ), @"." )
MutableStringAppendString( mutStr, @"\n" )
break
end if
string = mid( string, n2 + 1, len(string) )
c++
wend
print
end fn = mutStr
end fn
 
window 1, @"Odd word task in FutureBasic", (0,0,310,155)
print fn OddWordTask( @"what,is,the;meaning,of:life." )
print fn OddWordTaskoddWordTask( @"we,are;notwhat,inis,kansasthe;anymeaning,moreof:life." )
fn oddWordTask( @"we,are;not,in,kansas;any,more." )
fn oddWordTask( @"This also works with normal spaces." )
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:FB output for Odd Word Task.png]]
<pre>
Original text: what,is,the;meaning,of:life.
OddWord parse: what,si,the;gninaem,of:efil.
 
Original text: we,are;not,in,kansas;any,more.
OddWord parse: we,era;not,ni,kansas;yna,more.
 
</pre>
 
=={{header|Go}}==
Line 3,037 ⟶ 3,063:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin,Stdout
import "./str" for Char
 
var fwrite = Fn.new { |ch|
2,083

edits