Tuesday, May 10, 2011

Practice 6: Methods

Create the program
that you see on the right. The program
will allow long text to inputted into it through a textbox. When the user clicks "Analyze", it
will do 4 things:
  1. Count the total characters
  2. Count the words
  3. Count the sentences
  1. Count the spaces

I want each of these
counts to be calculated in their own functions. Name these functions according to what is happening inside of it.

You should also have
a sub who's job is to update the text boxes with the proper counts.

You will have to do
some googling to figure out how to do word and sentence counts. Spaces and total chars should be pretty
straight forward.

 Public Class Form1
  
   Public Function SentenceSetup(ByVal InputString As String) As Integer
  
     Dim period, qm, ex As Integer
  
     period = InputString.Split(".").Length
  
     qm = InputString.Split("?").Length
  
     ex = InputString.Split("!").Length
  
     Return (period + qm + ex) / 2
  
   End Function
  
   Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
  
     'Count characters
  
     txtChar.Text = AnalyzeText.Text.Length.ToString
  
     'Count words
  
     Dim str As String = AnalyzeText.Text
  
     Dim strA() As String = str.Split(" ")
  
     Dim TChrs As Int32
  
     Dim i As Int32
  
     For i = strA.Length - 1 To 0 Step -1
  
       TChrs += strA(i).Length
  
     Next
  
     txtWords.Text = strA.Length
  
     'Count spaces
  
     Dim Spaces As Integer = 0
  
     For Each s As String In AnalyzeText.Text
  
       If s = " " Then
  
         Spaces += 1
  
       End If
  
     Next
  
     txtSpace.Text = Spaces
  
     'Count Sentences
  
     txtSentence.Text = SentenceSetup(AnalyzeText.Text)
  
   End Sub
  
 End Class  

No comments:

Post a Comment