Odd word problem: Difference between revisions

no edit summary
(Add Julia language)
No edit summary
Line 2,463:
we,era;not,ni,kansas;yna,more.
</pre>
 
=={{header|VBA}}==
First way :
<lang vb>Private Function OddWordFirst(W As String) As String
Dim i As Integer, count As Integer, l As Integer, flag As Boolean, temp As String
count = 1
Do
flag = Not flag
l = FindNextPunct(i, W) - count + 1
If flag Then
temp = temp & ExtractWord(W, count, l)
Else
temp = temp & ReverseWord(W, count, l)
End If
Loop While count < Len(W)
OddWordFirst = temp
End Function
 
Private Function FindNextPunct(d As Integer, W As String) As Integer
Const PUNCT As String = ",;:."
Do
d = d + 1
Loop While InStr(PUNCT, Mid(W, d, 1)) = 0
FindNextPunct = d
End Function
 
Private Function ExtractWord(W As String, c As Integer, i As Integer) As String
ExtractWord = Mid(W, c, i)
c = c + Len(ExtractWord)
End Function
 
Private Function ReverseWord(W As String, c As Integer, i As Integer) As String
Dim temp As String, sep As String
temp = Left(Mid(W, c, i), Len(Mid(W, c, i)) - 1)
sep = Right(Mid(W, c, i), 1)
ReverseWord = StrReverse(temp) & sep
c = c + Len(ReverseWord)
End Function</lang>
Second way :
<lang vb>Private Function OddWordSecond(Words As String) As String
Dim i&, count&, t$, cpt&, j&, l&, d&, f As Boolean
Const PUNCT As String = ",;:"
For i = 1 To Len(Words)
'first word
If i = 1 Then
cpt = 1
Do
t = t & Mid(Words, cpt, 1)
cpt = cpt + 1
Loop While InStr(PUNCT, Mid(Words, cpt, 1)) = 0 And cpt < Len(Words)
i = cpt
t = t & Mid(Words, i, 1)
End If
If Right(t, 1) = "." Then Exit For
'Odd words ==> reverse
While InStr(PUNCT, Mid(Words, cpt + 1, 1)) = 0 And cpt < Len(Words)
cpt = cpt + 1
Wend
l = IIf(f = True, i, i + 1)
d = IIf(cpt = Len(Words), cpt - 1, cpt)
For j = d To l Step -1
t = t & Mid(Words, j, 1)
Next
If cpt = Len(Words) Then t = t & ".": Exit For
f = True
i = cpt + 1
t = t & Mid(Words, i, 1)
'Even words
cpt = i + 1
t = t & Mid(Words, cpt, 1)
Do
cpt = cpt + 1
t = t & Mid(Words, cpt, 1)
Loop While InStr(PUNCT, Mid(Words, cpt, 1)) = 0 And cpt < Len(Words)
i = cpt
Next
OddWordSecond = t
End Function</lang>
To call Functions :
<lang vb>Option Explicit
 
Sub Main()
Debug.Print "Input : " & "we,are;not,in,kansas;any,more."
Debug.Print "First way : " & OddWordFirst("we,are;not,in,kansas;any,more.")
Debug.Print "Second way : " & OddWordSecond("we,are;not,in,kansas;any,more.")
Debug.Print ""
Debug.Print "Input : " & "what,is,the;meaning,of:life."
Debug.Print "First way : " & OddWordFirst("what,is,the;meaning,of:life.")
Debug.Print "Second way : " & OddWordSecond("what,is,the;meaning,of:life.")
End Sub</lang>
{{out}}
<pre>Input : we,are;not,in,kansas;any,more.
First way : we,era;not,ni,kansas;yna,more.
Second way : we,era;not,ni,kansas;yna,more.
 
Input : what,is,the;meaning,of:life.
First way : what,si,the;gninaem,of:efil.
Second way : what,si,the;gninaem,of:efil.</pre>
 
=={{header|zkl}}==
Anonymous user