473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. Public Class frmPigLatin
  2.  
  3.     Private Sub btnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTranslate.Click
  4.         Dim strChar1 As String
  5.         Dim strInput As String
  6.         Dim strOutput As String = ""
  7.         Dim strWords As String()  'Makes an empty array
  8.         Dim i As Integer
  9.         Dim strConversion As String
  10.         strInput = txtEnterWords.Text
  11.  
  12.         'Split the text into an array of words
  13.         strWords = Split(strInput, " ")
  14.  
  15.         For i = LBound(strWords) To UBound(strWords)
  16.             strChar1 = strWords(i).Substring(0, 1)
  17.  
  18.             Select Case strChar1.ToUpper
  19.                 Case "A", "E", "I", "O", "U"
  20.                     strWords(i) = (strWords(i) & "WAY")
  21.                 Case Else
  22.                     strWords(i) = (strWords(i).Substring(1) & strChar1 & "AY")
  23.             End Select
  24.  
  25.             'Loop through each word...  
  26.         Next i
  27.  
  28.         'Put the words back together. 
  29.         strConversion = String.Join(" ", strWords)
  30.         MessageBox.Show(strConversion _
  31.         & vbCrLf & "There are " & nbrWords() & " words in the box.", "Pig Latin")
  32.  
  33.     End Sub
  34.  
  35.     Private Function nbrWords()
  36.  
  37.         Dim strLength As String
  38.         Dim strWhole As String
  39.         Dim strCounter As String
  40.         Dim x As Integer
  41.  
  42.         'Trim spaces on front and end.
  43.         strWhole = Trim(Me.txtEnterWords.Text)
  44.         If strWhole = "" Then
  45.             MsgBox("Please enter words")
  46.         End If
  47.         'Put the length of strWhole in the strLength variable
  48.         strLength = Len(strWhole)
  49.         strCounter = 1
  50.  
  51.         Dim currentLetter As String
  52.         Dim prevLetter As String
  53.  
  54.         For x = 2 To strLength
  55.             currentLetter = Mid(strWhole, x, 1)
  56.             prevLetter = Mid(strWhole, x - 1, 1)
  57.             If currentLetter = Chr(32) And prevLetter <> Chr(32) Then
  58.                 strCounter = strCounter + 1
  59.             End If
  60.  
  61.         Next
  62.  
  63.         Return strCounter
  64.  
  65.     End Function
  66.  
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?
Mar 6 '08 #1
9 1309
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?

Expand|Select|Wrap|Line Numbers
  1.         'Split the text into an array of words
  2.         strWords = Split(strInput, " ")
  3.  
  4.         For i = LBound(strWords) To UBound(strWords)
  5.             strChar1 = strWords(i).Substring(0, 1)
  6.  
  7.             Select Case strChar1.ToUpper
  8.                 Case "A", "E", "I", "O", "U"
  9.                     strWords(i) = (strWords(i) & "WAY")
  10.                 Case Else
  11.                     strWords(i) = (strWords(i).Substring(1) & strChar1 & "AY")
  12.             End Select
  13.  
  14.             'Loop through each word...  
  15.         Next i
  16.  
Mar 7 '08 #2
Killer42
8,435 Expert 8TB
Wouldn't you just increment a numeric variable after, say, line 9?
Mar 7 '08 #3
sigh, thank you killer.. appreciate it.
Mar 7 '08 #4
Killer42
8,435 Expert 8TB
sigh, thank you killer.. appreciate it.
The simple ones are surprisingly easy to miss, huh.
Mar 7 '08 #5
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 :/


Expand|Select|Wrap|Line Numbers
  1.         Try
  2.             For i = LBound(strWords) To UBound(strWords)
  3.  
  4.                 strChar1 = strWords(i).Substring(0, 1)
  5.  
  6.                 Select Case strChar1.ToUpper
  7.                     Case "A", "E", "I", "O", "U"
  8.                         strWords(i) = (strWords(i) & "WAY")
  9.                         intCountVowel = intCountVowel + 1
  10.  
  11.                     Case Else
  12.                         strWords(i) = (strWords(i).Substring(1) & strChar1 & "AY")
  13.                 End Select
  14.  
  15.                 'Loop through each word...  
  16.             Next i
  17.         Catch Sentance As SystemException
  18.             MessageBox.Show("Please Enter a valid sentence.")
  19.             Me.txtEnterWords.Focus()
  20.         End Try
  21.  
  22.  
Mar 8 '08 #6
QVeen72
1,445 Expert 1GB
Hi,

On 11th line write this :

If Trim(txtEnterWords.Text) ="" Then Exit Sub

Regards
Veena
Mar 8 '08 #7
didnt work, it gives me the enter a sentence, but when i click ok. It goes down to the message statements and executes that :/
Mar 8 '08 #8
QVeen72
1,445 Expert 1GB
Hi,

In your Original function write this after 43rd line:

Expand|Select|Wrap|Line Numbers
  1. If Trim(txtEnterWords.Text) ="" Then 
  2.    strCounter = 0
  3.    Return strCounter
  4.    Exit Function
  5. End If
  6.  
Regards
Veena
Mar 8 '08 #9
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!
Mar 8 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: cassandra.flowers | last post by:
Hi, I'm using Visual Basic 6 and I'm trying to make a program to change the colours of traffic lights. I am using 3 oval shapes, for each light. I want the traffic lights to change colour...
15
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
8
by: bmlclemson08 | last post by:
Hey if anyone could I need to find out how to write a program that will read in any number of integers, including none, and determine which is the largest integer. The part i can't figure out is...
7
by: news.east.cox.net | last post by:
Hello, I'm trying to figure out why the following code won't work for me. The Firefox javascript console tells me that tableItemClicked is not defined, but the function is right there in the...
2
by: cJ500 | last post by:
I can't figure out the problem with this code. I'm using a Borland compiler. The problem is with the / operation overload. When I call isNeg it doesn't output true or false. Now I know this code...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
2
by: Zerofury | last post by:
Okay this is what i'm attempting to do. I have to modify this program that i wrote so that it allows the user to list items by alpha as an option on the main menu. Here is my problem. If i sort the...
0
by: yjh0914 | last post by:
hi guys! so im basically editting my post i made earlier as it wznt as specific.. i have to make a program that basically ranks students by their cumulative gpa. the student's info is on a csv file...
2
by: ccarter45 | last post by:
Okay, so I need to write this program that first declares an array and inputs the number of each snake present on a plane (there are 5 diff. snakes). Next, it must print out the number of each snake:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.