' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
﻿Imports System
Imports System.Linq

Module Program
    Function CountWord(inputString As String) As Integer
        Dim pos As Integer, spaceCount As Integer
        pos = 1
        spaceCount = 1
        While Not pos = Len(inputString)
            If Mid(inputString, pos, 1) = " " Then
                If Not pos = 1 Then
                    If Not Mid(inputString, pos - 1, 1) = " " Then
                        spaceCount = spaceCount + 1
                    End If
                End If
            End If
            pos = pos + 1
        End While
        CountWord = spaceCount
    End Function  
    Function CountWordAltA(s As String) As Integer
        Return s.Trim().Split(" "C).Length
    End Function
    Function CountWordAltB(s As String) As Integer
        Return s.Trim().ToCharArray().Count(Function(c) c = " "C) + 1
    End Function
    Sub Test(s As String)
        Console.WriteLine(CountWord(s))
        Console.WriteLine(CountWordAltA(s))
        Console.WriteLine(CountWordAltB(s))
    End Sub
    Sub Main()
        Test("A B C")
        Test(" A B C ")
    End Sub
End Module
