Task

Write a function/procedure/subroutine that is called with an integer value and returns the middle three digits of the integer if possible or a clear indication of an error if this is not possible.

Note: The order of the middle digits should be preserved.

Your function should be tested with the following values; the first line should return valid answers, those of the second line should return clear indications of an error:

123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345
1, 2, -1, -10, 2002, -2002, 0

Show your output on this page.




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