-
Using Instr in a loop ... help
Hi - I'm new to VB and I was trying to write a function that accepts a string
as an argument and then returns as an integer value the number of vowels
in the string. Below is my attempt, but I can't get it to work for more then
one vowel e.g "E".
Thanks in advance
Kev
Function Vowelcounts(n As String) As Integer
totalvowel = 0
X = 0
i = 0
If X < 1 Then X = 1
For c = 0 To Len(n)
i = InStr(X, n, "E")
If i = 0 Then Exit Function
X = i + 1
totalvowel = totalvowel + 1
Vowelcounts = totalvowel
Next
End Function
-
Re: Using Instr in a loop ... help
Kevin Galvin wrote in message <39a4fb9b$1@news.devx.com>...
>
>Hi - I'm new to VB and I was trying to write a function that accepts a
string
>as an argument and then returns as an integer value the number of vowels
>in the string. Below is my attempt, but I can't get it to work for more
then
>one vowel e.g "E".
>
>Thanks in advance
>
>Kev
Why not modularize that function a little bit so that it'll take in a
character as a parameter, and return the number of times that character is
in the string as the output? Then you can just call the function five (or
six, if you're counting 'y') times, and add the total up.
--
Colin McGuigan
-
Re: Using Instr in a loop ... help
The Like operator can be very handy for constructions like you are
suggesting; you should try and become familiar with it. This function should
do what you seek:
Function NumberOfVowels(StringIn As String) As Long
For x = 1 To Len(StringIn)
If Mid$(StringIn, x, 1) Like "[aeiou]" Then
NumberOfVowels = NumberOfVowels + 1
End If
Next
End Function
Side Note: If this question is a homework assignment, then the above will
probably be useless to you as no instructor asking you to answer a question
like this would believe you "tripped" across the Like operator at the stage
of development he/she would know you to be at.
Rick
"Kevin Galvin" <kevingalvin@hotmail.com> wrote in message
news:39a4fb9b$1@news.devx.com...
>
> Hi - I'm new to VB and I was trying to write a function that accepts a
string
> as an argument and then returns as an integer value the number of vowels
> in the string. Below is my attempt, but I can't get it to work for more
then
> one vowel e.g "E".
>
> Thanks in advance
>
> Kev
>
> Function Vowelcounts(n As String) As Integer
>
> totalvowel = 0
>
> X = 0
> i = 0
>
> If X < 1 Then X = 1
>
> For c = 0 To Len(n)
>
> i = InStr(X, n, "E")
> If i = 0 Then Exit Function
>
> X = i + 1
>
> totalvowel = totalvowel + 1
> Vowelcounts = totalvowel
>
> Next
>
> End Function
>
-
Re: Using Instr in a loop ... help
Kevin,
use Do ... While Instead of For ... Next
X = 0
Do
i = InStr(X + 1, n, "E")
If i > 0 Then
X = i
...
...
...
End If
Loop While i > 0
Alex
"Kevin Galvin" <kevingalvin@hotmail.com> wrote:
>
>Hi - I'm new to VB and I was trying to write a function that accepts a string
>as an argument and then returns as an integer value the number of vowels
>in the string. Below is my attempt, but I can't get it to work for more
then
>one vowel e.g "E".
>
>Thanks in advance
>
>Kev
>
>Function Vowelcounts(n As String) As Integer
>
>totalvowel = 0
>
>X = 0
>i = 0
>
>If X < 1 Then X = 1
>
>For c = 0 To Len(n)
>
>i = InStr(X, n, "E")
>If i = 0 Then Exit Function
>
>X = i + 1
>
>totalvowel = totalvowel + 1
>Vowelcounts = totalvowel
>
>Next
>
>End Function
>
-
Re: Using Instr in a loop ... help
Yo Rick, what if his teacher grades him on missing the 'Y'.
Okay - flame away! <g>
Sheldon
Rick Rothstein <rick_newsgroup@email.com> wrote in message
news:39a51135$1@news.devx.com...
> The Like operator can be very handy for constructions like you are
> suggesting; you should try and become familiar with it. This function
should
> do what you seek:
>
> Function NumberOfVowels(StringIn As String) As Long
> For x = 1 To Len(StringIn)
> If Mid$(StringIn, x, 1) Like "[aeiou]" Then
> NumberOfVowels = NumberOfVowels + 1
> End If
> Next
> End Function
>
> Side Note: If this question is a homework assignment, then the above will
> probably be useless to you as no instructor asking you to answer a
question
> like this would believe you "tripped" across the Like operator at the
stage
> of development he/she would know you to be at.
>
> Rick
>
>
> "Kevin Galvin" <kevingalvin@hotmail.com> wrote in message
> news:39a4fb9b$1@news.devx.com...
> >
> > Hi - I'm new to VB and I was trying to write a function that accepts a
> string
> > as an argument and then returns as an integer value the number of vowels
> > in the string. Below is my attempt, but I can't get it to work for more
> then
> > one vowel e.g "E".
> >
> > Thanks in advance
> >
> > Kev
> >
> > Function Vowelcounts(n As String) As Integer
> >
> > totalvowel = 0
> >
> > X = 0
> > i = 0
> >
> > If X < 1 Then X = 1
> >
> > For c = 0 To Len(n)
> >
> > i = InStr(X, n, "E")
> > If i = 0 Then Exit Function
> >
> > X = i + 1
> >
> > totalvowel = totalvowel + 1
> > Vowelcounts = totalvowel
> >
> > Next
> >
> > End Function
> >
>
>
-
Re: Using Instr in a loop ... help
Kevin,
While Rick's suggestion is probably more efficient, the following technique
can be expanded upon to become useful in other situations:
Public Function CountVowels(ByVal sTestString As String) As Long
Dim lCounter As Long
Dim lNumVowels As Long
sTestString = UCase$(sTestString)
lCounter = 1
For lCounter = 1 To Len(sTestString)
Select Case Mid$(sTestString, lCounter, 1)
Case "A", "E", "I", "O", "U", "Y"
lNumVowels = lNumVowels +1
End Select
Next
CountVowels = lNumVowels
End Function
--Kenny Acock
"Kevin Galvin" <kevingalvin@hotmail.com> wrote:
>
>Hi - I'm new to VB and I was trying to write a function that accepts a string
>as an argument and then returns as an integer value the number of vowels
>in the string. Below is my attempt, but I can't get it to work for more
then
>one vowel e.g "E".
>
>Thanks in advance
>
>Kev
>
>Function Vowelcounts(n As String) As Integer
>
>totalvowel = 0
>
>X = 0
>i = 0
>
>If X < 1 Then X = 1
>
>For c = 0 To Len(n)
>
>i = InStr(X, n, "E")
>If i = 0 Then Exit Function
>
>X = i + 1
>
>totalvowel = totalvowel + 1
>Vowelcounts = totalvowel
>
>Next
>
>End Function
>
-
Re: Using Instr in a loop ... help
Good catch, Shelly! Since y is either a vowel or a consonant, the only way
to determine which it is, is by examining the context in which the y is used.
Perhaps a dictionary or grammar component is what is needed. And not just
y - w can also be a vowel. Shouldn't we account for w as well? Also, do
we want to make this routine internationally aware? The old "a, e, i, o,
u and sometimes w and y" rule may not apply to foreign languages. This can
get quite complicated 
- Jim
"Shelly Rosenfeld" <ShellyRo@worldnet.att.net> wrote:
>
>Yo Rick, what if his teacher grades him on missing the 'Y'.
>
>Okay - flame away! <g>
>
>Sheldon
>
>
>Rick Rothstein <rick_newsgroup@email.com> wrote in message
>news:39a51135$1@news.devx.com...
>> The Like operator can be very handy for constructions like you are
>> suggesting; you should try and become familiar with it. This function
>should
>> do what you seek:
>>
>> Function NumberOfVowels(StringIn As String) As Long
>> For x = 1 To Len(StringIn)
>> If Mid$(StringIn, x, 1) Like "[aeiou]" Then
>> NumberOfVowels = NumberOfVowels + 1
>> End If
>> Next
>> End Function
>>
>> Side Note: If this question is a homework assignment, then the above will
>> probably be useless to you as no instructor asking you to answer a
>question
>> like this would believe you "tripped" across the Like operator at the
>stage
>> of development he/she would know you to be at.
>>
>> Rick
>>
>>
>> "Kevin Galvin" <kevingalvin@hotmail.com> wrote in message
>> news:39a4fb9b$1@news.devx.com...
>> >
>> > Hi - I'm new to VB and I was trying to write a function that accepts
a
>> string
>> > as an argument and then returns as an integer value the number of vowels
>> > in the string. Below is my attempt, but I can't get it to work for more
>> then
>> > one vowel e.g "E".
>> >
>> > Thanks in advance
>> >
>> > Kev
>> >
>> > Function Vowelcounts(n As String) As Integer
>> >
>> > totalvowel = 0
>> >
>> > X = 0
>> > i = 0
>> >
>> > If X < 1 Then X = 1
>> >
>> > For c = 0 To Len(n)
>> >
>> > i = InStr(X, n, "E")
>> > If i = 0 Then Exit Function
>> >
>> > X = i + 1
>> >
>> > totalvowel = totalvowel + 1
>> > Vowelcounts = totalvowel
>> >
>> > Next
>> >
>> > End Function
>> >
>>
>>
>
>
-
Re: Using Instr in a loop ... help
Just for completeness sake (as a result of reading a parallel post by Kenny
Acock), I should have provided for the case of the letters (upper or lower).
Hence, change this line
If Mid$(StringIn, x, 1) Like "[aeiou]" Then
to this instead
If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
which should be more efficient than
If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
Rick
"Rick Rothstein" <rick_newsgroup@email.com> wrote in message
news:39a51135$1@news.devx.com...
> The Like operator can be very handy for constructions like you are
> suggesting; you should try and become familiar with it. This function
should
> do what you seek:
>
> Function NumberOfVowels(StringIn As String) As Long
> For x = 1 To Len(StringIn)
> If Mid$(StringIn, x, 1) Like "[aeiou]" Then
> NumberOfVowels = NumberOfVowels + 1
> End If
> Next
> End Function
>
> Side Note: If this question is a homework assignment, then the above will
> probably be useless to you as no instructor asking you to answer a
question
> like this would believe you "tripped" across the Like operator at the
stage
> of development he/she would know you to be at.
>
> Rick
>
>
> "Kevin Galvin" <kevingalvin@hotmail.com> wrote in message
> news:39a4fb9b$1@news.devx.com...
> >
> > Hi - I'm new to VB and I was trying to write a function that accepts a
> string
> > as an argument and then returns as an integer value the number of vowels
> > in the string. Below is my attempt, but I can't get it to work for more
> then
> > one vowel e.g "E".
> >
> > Thanks in advance
> >
> > Kev
> >
> > Function Vowelcounts(n As String) As Integer
> >
> > totalvowel = 0
> >
> > X = 0
> > i = 0
> >
> > If X < 1 Then X = 1
> >
> > For c = 0 To Len(n)
> >
> > i = InStr(X, n, "E")
> > If i = 0 Then Exit Function
> >
> > X = i + 1
> >
> > totalvowel = totalvowel + 1
> > Vowelcounts = totalvowel
> >
> > Next
> >
> > End Function
> >
>
>
-
Re: Using Instr in a loop ... help
No flames <g>. I wasn't sure what to do about "Y" since it isn't always a
vowel. I decided to take the easy way out and ignore it (thus allowing Kevin
the room to patch it in if he decided it was necessary).
Rick
"Shelly Rosenfeld" <ShellyRo@worldnet.att.net> wrote in message
news:39a530bc@news.devx.com...
>
> Yo Rick, what if his teacher grades him on missing the 'Y'.
>
> Okay - flame away! <g>
>
> Sheldon
>
>
> Rick Rothstein <rick_newsgroup@email.com> wrote in message
> news:39a51135$1@news.devx.com...
> > The Like operator can be very handy for constructions like you are
> > suggesting; you should try and become familiar with it. This function
> should
> > do what you seek:
> >
> > Function NumberOfVowels(StringIn As String) As Long
> > For x = 1 To Len(StringIn)
> > If Mid$(StringIn, x, 1) Like "[aeiou]" Then
> > NumberOfVowels = NumberOfVowels + 1
> > End If
> > Next
> > End Function
> >
> > Side Note: If this question is a homework assignment, then the above
will
> > probably be useless to you as no instructor asking you to answer a
> question
> > like this would believe you "tripped" across the Like operator at the
> stage
> > of development he/she would know you to be at.
> >
> > Rick
> >
> >
> > "Kevin Galvin" <kevingalvin@hotmail.com> wrote in message
> > news:39a4fb9b$1@news.devx.com...
> > >
> > > Hi - I'm new to VB and I was trying to write a function that accepts a
> > string
> > > as an argument and then returns as an integer value the number of
vowels
> > > in the string. Below is my attempt, but I can't get it to work for
more
> > then
> > > one vowel e.g "E".
> > >
> > > Thanks in advance
> > >
> > > Kev
> > >
> > > Function Vowelcounts(n As String) As Integer
> > >
> > > totalvowel = 0
> > >
> > > X = 0
> > > i = 0
> > >
> > > If X < 1 Then X = 1
> > >
> > > For c = 0 To Len(n)
> > >
> > > i = InStr(X, n, "E")
> > > If i = 0 Then Exit Function
> > >
> > > X = i + 1
> > >
> > > totalvowel = totalvowel + 1
> > > Vowelcounts = totalvowel
> > >
> > > Next
> > >
> > > End Function
> > >
> >
> >
>
>
-
Re: Using Instr in a loop ... help
Ah yes, the "W" vowel. I had forgotten all about that one. For those of you
searching for an example of a word where "W" is a vowel, look up "CWM" in a
reasonably decent dictionary (talking US here -- apologies to the rest of
the world <g>).
Rick
"Jim Pragit" <NoSpam@NoSpam.com> wrote in message
news:39a540e1$1@news.devx.com...
>
> Good catch, Shelly! Since y is either a vowel or a consonant, the only
way
> to determine which it is, is by examining the context in which the y is
used.
> Perhaps a dictionary or grammar component is what is needed. And not
just
> y - w can also be a vowel. Shouldn't we account for w as well? Also, do
> we want to make this routine internationally aware? The old "a, e, i, o,
> u and sometimes w and y" rule may not apply to foreign languages. This
can
> get quite complicated 
>
> - Jim
>
> "Shelly Rosenfeld" <ShellyRo@worldnet.att.net> wrote:
> >
> >Yo Rick, what if his teacher grades him on missing the 'Y'.
> >
> >Okay - flame away! <g>
> >
> >Sheldon
> >
> >
> >Rick Rothstein <rick_newsgroup@email.com> wrote in message
> >news:39a51135$1@news.devx.com...
> >> The Like operator can be very handy for constructions like you are
> >> suggesting; you should try and become familiar with it. This function
> >should
> >> do what you seek:
> >>
> >> Function NumberOfVowels(StringIn As String) As Long
> >> For x = 1 To Len(StringIn)
> >> If Mid$(StringIn, x, 1) Like "[aeiou]" Then
> >> NumberOfVowels = NumberOfVowels + 1
> >> End If
> >> Next
> >> End Function
> >>
> >> Side Note: If this question is a homework assignment, then the above
will
> >> probably be useless to you as no instructor asking you to answer a
> >question
> >> like this would believe you "tripped" across the Like operator at the
> >stage
> >> of development he/she would know you to be at.
> >>
> >> Rick
> >>
> >>
> >> "Kevin Galvin" <kevingalvin@hotmail.com> wrote in message
> >> news:39a4fb9b$1@news.devx.com...
> >> >
> >> > Hi - I'm new to VB and I was trying to write a function that accepts
> a
> >> string
> >> > as an argument and then returns as an integer value the number of
vowels
> >> > in the string. Below is my attempt, but I can't get it to work for
more
> >> then
> >> > one vowel e.g "E".
> >> >
> >> > Thanks in advance
> >> >
> >> > Kev
> >> >
> >> > Function Vowelcounts(n As String) As Integer
> >> >
> >> > totalvowel = 0
> >> >
> >> > X = 0
> >> > i = 0
> >> >
> >> > If X < 1 Then X = 1
> >> >
> >> > For c = 0 To Len(n)
> >> >
> >> > i = InStr(X, n, "E")
> >> > If i = 0 Then Exit Function
> >> >
> >> > X = i + 1
> >> >
> >> > totalvowel = totalvowel + 1
> >> > Vowelcounts = totalvowel
> >> >
> >> > Next
> >> >
> >> > End Function
> >> >
> >>
> >>
> >
> >
>
-
Re: Using Instr in a loop ... help
Out of curiousity (and because I'm anal), I ran a benchmark and found this
line of code:
If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
to be about 30-40% faster than this line of code:
If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
For what it's worth, Jim
"Rick Rothstein" <rick_newsgroup@email.com> wrote:
>Just for completeness sake (as a result of reading a parallel post by Kenny
>Acock), I should have provided for the case of the letters (upper or lower).
>Hence, change this line
>
> If Mid$(StringIn, x, 1) Like "[aeiou]" Then
>
>to this instead
>
> If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
>
>which should be more efficient than
>
> If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
>
>Rick
-
Re: Using Instr in a loop ... help
Jim Pragit wrote in message <39a54964$1@news.devx.com>...
>
>Out of curiousity (and because I'm anal), I ran a benchmark and found this
>line of code:
>
> If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
>
>to be about 30-40% faster than this line of code:
>
> If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
>
>For what it's worth, Jim
And what happens if you use Option Compare Text?
--
Colin McGuigan
-
Re: Using Instr in a loop ... help
I have to test for a specific font on one of my forms;
if not present, I hide the textbox.
Anyway, I initially ran ucase$(screenfont) = reqdfont,
and it took so long to load a form!
Changing to StrComp gave me an unbelievable
increase in speed.
Lesson for today class - don't use lcase/ucase in
a loop...
Sheldon
Colin McGuigan <colin@chicor.com> wrote in message
news:39a54aaf$1@news.devx.com...
> Jim Pragit wrote in message <39a54964$1@news.devx.com>...
> >
> >Out of curiousity (and because I'm anal), I ran a benchmark and found
this
> >line of code:
> >
> > If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
> >
> >to be about 30-40% faster than this line of code:
> >
> > If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
> >
> >For what it's worth, Jim
>
>
> And what happens if you use Option Compare Text?
>
>
> --
> Colin McGuigan
>
>
>
-
Re: Using Instr in a loop ... help
Thanks for the timing-test. I figured (read as *too lazy* to test) the extra
string operation would drag the processing down and now I know I was right.
Much appreciated.
Rick
"Jim Pragit" <NoSpam@NoSpam.com> wrote in message
news:39a54964$1@news.devx.com...
>
> Out of curiousity (and because I'm anal), I ran a benchmark and found this
> line of code:
>
> If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
>
> to be about 30-40% faster than this line of code:
>
> If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
>
> For what it's worth, Jim
>
> "Rick Rothstein" <rick_newsgroup@email.com> wrote:
> >Just for completeness sake (as a result of reading a parallel post by
Kenny
> >Acock), I should have provided for the case of the letters (upper or
lower).
> >Hence, change this line
> >
> > If Mid$(StringIn, x, 1) Like "[aeiou]" Then
> >
> >to this instead
> >
> > If Mid$(StringIn, x, 1) Like "[aeiouAEIOU]" Then
> >
> >which should be more efficient than
> >
> > If LCase$(Mid$(StringIn, x, 1)) Like "[aeiou]" Then
> >
> >Rick
>
>
-
Re: Using Instr in a loop ... help
"Kevin Galvin" <kevingalvin@hotmail.com> wrote:
>
>Hi - I'm new to VB and I was trying to write a function that accepts a string
>as an argument and then returns as an integer value the number of vowels
>in the string. Below is my attempt, but I can't get it to work for more
then
>one vowel e.g "E".
>
>Thanks in advance
>
>Kev
>
>Function Vowelcounts(n As String) As Integer
>
>totalvowel = 0
>
>X = 0
>i = 0
>
>If X < 1 Then X = 1
>
>For c = 0 To Len(n)
>
>i = InStr(X, n, "E")
>If i = 0 Then Exit Function
>
>X = i + 1
>
>totalvowel = totalvowel + 1
>Vowelcounts = totalvowel
>
>Next
>
>End Function
>
When you find the first item you are looking for with instr update you loop
control variable with the position of the sub string.
Function Vowelcounts(n As String) As Integer
totalvowel = 0
X = 0
i = 0
If X < 1 Then X = 1
For c = 1 To Len(n)
i = InStr(c, n, "E")
If i = 0 Then Exit Function
c = i '
totalvowel = totalvowel + 1
Vowelcounts = totalvowel
Next
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|