RosettaCodeData/Task/Middle-three-digits/Gambas/middle-three-digits.gambas
2017-09-25 22:28:19 +02:00

24 lines
1.5 KiB
Text

Public Sub Main()
Dim iList As Integer[] = [123, 12345, 1234567, 987654321, 10001,
-10001, -123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0] 'Array of integers to process
Dim sTemp As String 'Temp string
Dim siCount As Short 'Counter
Dim sAnswer As New String[] 'Array, resons for failure or 'middle three digits'
For siCount = 0 To iList.Max 'Loop through the integers
sTemp = Str(Abs(iList[siCount])) 'Convert integer to positive and place in sTemp as a string
If Len(sTemp) < 3 Then 'If sTemp has less than 3 characters then..
sAnswer.Add("This integer has less than 3 characters") 'Place text in sAnswers
Else If Even(Len(sTemp)) Then 'Else If sTemp is of even length then
sAnswer.Add("This integer has an even length") 'Place text in sAnswers
Else 'Else..
sAnswer.Add(Mid(sTemp, Int(Len(sTemp) / 2), 3)) 'Place the middle 3 digits in sAnswer
Endif
Next
For siCount = 0 To iList.Max 'Loop through the integers
Print Space$(10 - Len(Str(iList[siCount]))) &
iList[siCount] & " : " & sAnswer[siCount] 'Print out results
Next
End