help with counter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tragic54
    New Member
    • Feb 2008
    • 20

    help with counter

    Alright so i'm writing a program, and i need to count the number of times a word begins with a letter.

    Code:
    Public Class frmPigLatin
    
        Private Sub btnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTranslate.Click
            Dim strChar1 As String
            Dim strInput As String
            Dim strOutput As String = ""
            Dim strWords As String()  'Makes an empty array
            Dim i As Integer
            Dim strConversion As String
            strInput = txtEnterWords.Text
    
            'Split the text into an array of words
            strWords = Split(strInput, " ")
    
            For i = LBound(strWords) To UBound(strWords)
                strChar1 = strWords(i).Substring(0, 1)
    
                Select Case strChar1.ToUpper
                    Case "A", "E", "I", "O", "U"
                        strWords(i) = (strWords(i) & "WAY")
                    Case Else
                        strWords(i) = (strWords(i).Substring(1) & strChar1 & "AY")
                End Select
    
                'Loop through each word...  
            Next i
    
            'Put the words back together. 
            strConversion = String.Join(" ", strWords)
            MessageBox.Show(strConversion _
            & vbCrLf & "There are " & nbrWords() & " words in the box.", "Pig Latin")
    
        End Sub
    
        Private Function nbrWords()
    
            Dim strLength As String
            Dim strWhole As String
            Dim strCounter As String
            Dim x As Integer
    
            'Trim spaces on front and end.
            strWhole = Trim(Me.txtEnterWords.Text)
            If strWhole = "" Then
                MsgBox("Please enter words")
            End If
            'Put the length of strWhole in the strLength variable
            strLength = Len(strWhole)
            strCounter = 1
    
            Dim currentLetter As String
            Dim prevLetter As String
    
            For x = 2 To strLength
                currentLetter = Mid(strWhole, x, 1)
                prevLetter = Mid(strWhole, x - 1, 1)
                If currentLetter = Chr(32) And prevLetter <> Chr(32) Then
                    strCounter = strCounter + 1
                End If
    
            Next
    
            Return strCounter
    
        End Function
    Alright so now the message box displays the correct conversion to pig latin, and will count the number of words in the string, but i need to be pointed in the right direction as to how i could.

    1. Count the number of words that begin with a vowel coming from the text box. I was looking around and learned how to count all the vowels in the string, but i dont know what i need to do to search the first char then skip the string and goto the next. Can anyone point me in the right direction?
  • tragic54
    New Member
    • Feb 2008
    • 20

    #2
    Is it possible to insert a counter in this line of code to count if the string begins with vowel while its being split up?

    Code:
            'Split the text into an array of words
            strWords = Split(strInput, " ")
     
            For i = LBound(strWords) To UBound(strWords)
                strChar1 = strWords(i).Substring(0, 1)
     
                Select Case strChar1.ToUpper
                    Case "A", "E", "I", "O", "U"
                        strWords(i) = (strWords(i) & "WAY")
                    Case Else
                        strWords(i) = (strWords(i).Substring(1) & strChar1 & "AY")
                End Select
     
                'Loop through each word...  
            Next i

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Wouldn't you just increment a numeric variable after, say, line 9?

      Comment

      • tragic54
        New Member
        • Feb 2008
        • 20

        #4
        sigh, thank you killer.. appreciate it.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by tragic54
          sigh, thank you killer.. appreciate it.
          The simple ones are surprisingly easy to miss, huh.

          Comment

          • tragic54
            New Member
            • Feb 2008
            • 20

            #6
            One more question if someone doesnt mind.

            I'm trying to throw an exception if nothing is entered in the text box. The message is displayed once the translate button is clicked, but i need it to stop immediately before any output is given through the message box. And for some reason, focus won't work on my text box :/


            Code:
                    Try
                        For i = LBound(strWords) To UBound(strWords)
            
                            strChar1 = strWords(i).Substring(0, 1)
            
                            Select Case strChar1.ToUpper
                                Case "A", "E", "I", "O", "U"
                                    strWords(i) = (strWords(i) & "WAY")
                                    intCountVowel = intCountVowel + 1
            
                                Case Else
                                    strWords(i) = (strWords(i).Substring(1) & strChar1 & "AY")
                            End Select
            
                            'Loop through each word...  
                        Next i
                    Catch Sentance As SystemException
                        MessageBox.Show("Please Enter a valid sentence.")
                        Me.txtEnterWords.Focus()
                    End Try

            Comment

            • QVeen72
              Recognized Expert Top Contributor
              • Oct 2006
              • 1445

              #7
              Hi,

              On 11th line write this :

              If Trim(txtEnterWo rds.Text) ="" Then Exit Sub

              Regards
              Veena

              Comment

              • tragic54
                New Member
                • Feb 2008
                • 20

                #8
                didnt work, it gives me the enter a sentence, but when i click ok. It goes down to the message statements and executes that :/

                Comment

                • QVeen72
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1445

                  #9
                  Hi,

                  In your Original function write this after 43rd line:

                  [code=vb]
                  If Trim(txtEnterWo rds.Text) ="" Then
                  strCounter = 0
                  Return strCounter
                  Exit Function
                  End If
                  [/code]

                  Regards
                  Veena

                  Comment

                  • tragic54
                    New Member
                    • Feb 2008
                    • 20

                    #10
                    That didn't work either, went through the division to get the percentage of vowels and threw a dividebyzero/overflow exception. So i piddled with it and just wrapped the full code as a system exception and it catches everything. Maybe it woulda helped if i would've given you the full code lol. Sorry bout that, but i appreciate you trying. Thanks!

                    Comment

                    Working...