Odd word problem: Difference between revisions
Content deleted Content added
Added FreeBasic |
Initial FutureBasic task solution added |
||
Line 1,006: | Line 1,006: | ||
Input stream: we,are;not,in,kansas;any,more. |
Input stream: we,are;not,in,kansas;any,more. |
||
Output stream: we,era;not,ni,kansas;yna,more.</pre> |
Output stream: we,era;not,ni,kansas;yna,more.</pre> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
begin globals |
|||
NSInteger n1 = 1 |
|||
end globals |
|||
local fn ReverseString( string as CFStringRef ) as CFStringRef |
|||
NSInteger i, length = len(string) |
|||
CFMutableStringRef mutStr = fn MutableStringWithString( string ) |
|||
for i = 0 to length - 1 |
|||
CFRange range1 = fn CFRangeMake( i, 1 ) |
|||
CFRange range2 = fn CFRangeMake( length - i - 1, 1 ) |
|||
CFStringRef chr1 = fn StringSubstringWithRange( string, range1 ) |
|||
CFStringRef chr2 = fn StringSubstringWithRange( string, range2 ) |
|||
swap chr1, chr2 |
|||
MutableStringReplaceCharacters( mutStr, chr1, range1 ) |
|||
MutableStringReplaceCharacters( mutStr, chr2, range2 ) |
|||
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 |
|||
end fn = mutStr |
|||
print fn OddWordTask( @"what,is,the;meaning,of:life." ) |
|||
print fn OddWordTask( @"we,are;not,in,kansas;any,more." ) |
|||
NSLog( @"%@", fn WindowPrintViewString( 1 ) ) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<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}}== |
=={{header|Go}}== |