Function returning a value V2 Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function returning a value V2 12


Objetive:

Create a program whose Main must be like this:

public static void Main()
{
__Console.WriteLine("\"Hello, how are you\" contains {0} spaces", ____CountSpaces("Hello, how are you") );
}

CountSpaces is a function that you must define and that will be called from inside Main.

As you can see in the example, it must accept an string as a parameter, and it must return an integer number (the amount of spaces in that string).


Code:

Imports System
Public Class exercise100
    Public Shared Function CountSpaces(ByVal text As String) As Integer
        Dim countSpaces As Integer = 0
        Dim letter As String

        For i As Integer = 0 To text.Length - 1
            letter = text.Substring(i, 1)
            If letter = " " Then countSpaces += 1
        Next

        Return countSpaces
    End Function

    Public Shared Sub Main()
        Console.WriteLine("""Hello, how are you"" contains {0} spaces", CountSpaces("Hello, how are you"))
    End Sub
End Class