Word Count Program in VB6

In my previous tutorials, I had also discussed about how to have an word count using vb.net. And for now, we will create another version of this program that can count the number of words using vb6.0. Now, let's start this tutorial! 1. Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open. 2.Next, add only one Button named Count Number of Words and labeled it as "Compute". Add also one TextBox named Text1 that will serve as your input to write your sentence.You must design your interface like this: design 3. Now put this code for your code module. This code is for Command1_Click:
  1. Private Sub Command1_Click()
  2.  
  3.         Dim tmpNum As Integer
  4.         Const onespace As String = " "
  5.         Const twospace As String = "  "
  6.         Dim tmpWords() As String
  7.         Dim tmpText As String
  8.        
  9.         tmpNum = -1
  10.         tmpText = Text1.Text
  11.         Do Until tmpNum = 0
  12.             tmpNum = InStr(tmpText, twospace)
  13.             If tmpNum > 0 Then
  14.                 tmpText = Replace(tmpText, twospace, onespace)
  15.             End If
  16.         Loop
  17.              
  18.         tmpWords = Split(tmpText, onespace)
  19.        MsgBox "Number of Words: " & UBound(tmpWords) + 1
  20. End Sub

Explanation:

We have initialized variable tmpNum As Integer will be equal to -1,tmpText As String will hold the inputted text in the textbox,tmpWords() As String Array, constant variable onespace As String is equal to one space, and constant variable twospace As String is equal to two spaces. Next, we have created a Do Until loop that us until tmpNum is equal to 0. Then tmpNum will hold the value of inserting a two space in tmpText variable. If tmpNum > 0 then tmpText will now hold the value of tmpText with one space. Replace method returns a string in which a specified substring has been replaced with another substring a specified number of times. Then variable tmpWords as an array will hold the split value of tmpText with onespace. Split Function returns a zero-based, one-dimensional array containing a specified number of substrings. Then, tmpWords will now hold the array elements that has the number of words. UBound fuction returns the highest available subscript for the indicated dimension of an array.

Output:

output Download the source code and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below and hire me. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer STI College - Surigao City Mobile: 09488225971 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Add new comment