Hello : >
I'm using console application (VB.NET), using Visual Studio 2005.

I have a problem with my project, I'm building a vowel counter program, and faced a problem there with loops..
any one can help me with my project?,,
can I contact with the helper using a private message?

thanks alot in advance ..
I really need a help :/

I don't use messenger programs, but if you post the code you have we can take a look and see where your problem lies.

Member Avatar for iamthwee

It's pretty simple:

You have a string, you loop through each character of the string and increment the counter every time it encounters a vowel.

thank you both for your answering my question : )
i'll come back and continue my question : )

How about this (if your strings are relatively short):

To get a count of each vowel without 'walking the string':

' strMyText is the string returned from the console
 
dim strMyTextCaps = strMyText.toUpper()  'We don't care about case
dim intTextLen = strMyText.length ' So we don't reevaluate every time
 
dim intCountA = intTextLen - (strMyTextCaps.Replace("A","").length)
dim intCountE = intTextLen - (strMyTextCaps.Replace("E","").length)
' etc...

It should be fairly obvious, but what we're doing is getting the length of the original string, and then replacing each vowel with an empty string. We subtract the new length from the original length and know the number of characters replaced. This is the vowel count for that vowel.

Haven't benchmarked for performance (nor have I run this - I believe the syntax is correct). A single line for each vowel is easier to read than a bunch of loops. Performance should not be a problem anyway, if the text is provided via the console.

Happy Coding!

Ned

Member Avatar for iamthwee

How about this (if your strings are relatively short):

To get a count of each vowel without 'walking the string':

' strMyText is the string returned from the console
 
dim strMyTextCaps = strMyText.toUpper()  'We don't care about case
dim intTextLen = strMyText.length ' So we don't reevaluate every time
 
dim intCountA = intTextLen - (strMyTextCaps.Replace("A","").length)
dim intCountE = intTextLen - (strMyTextCaps.Replace("E","").length)
' etc...

It should be fairly obvious, but what we're doing is getting the length of the original string, and then replacing each vowel with an empty string. We subtract the new length from the original length and know the number of characters replaced. This is the vowel count for that vowel.

Haven't benchmarked for performance (nor have I run this - I believe the syntax is correct). A single line for each vowel is easier to read than a bunch of loops. Performance should not be a problem anyway, if the text is provided via the console.

Happy Coding!

Ned

If you're replacing every vowel with the same "", how would you tell the difference between each vowel?

Wait unless you're reading the string five times in, i.e the number of vowels there are aeiou?

We don't care about the number of "",s we care about the difference between the old length (with the vowels intact) and the new length (with one of the vowels converted to ""). Example:

Text = "I am here"
Length of original Text = 9
Text after Replace("a","") = "I m here"
Length of new Text = 8
Difference = 1, therefore Count of A's in the text = 1.

We can ignore leading and trailing blanks because we are not counting any characters - we're replacing all occurances of one character with a no-space and getting the difference in lengths.

Member Avatar for iamthwee

We don't care about the number of "",s we care about the difference between the old length (with the vowels intact) and the new length (with one of the vowels converted to ""). Example:

Text = "I am here"
Length of original Text = 9
Text after Replace("a","") = "I m here"
Length of new Text = 8
Difference = 1, therefore Count of A's in the text = 1.

We can ignore leading and trailing blanks because we are not counting any characters - we're replacing all occurances of one character with a no-space and getting the difference in lengths.

Yes I just realised that and have since edited my post, however, your program requires you to read the string in five times.

Indeed, it does. Here's where hard-won philosophy of programming comes in (I don't know your experience, but I've been at this most of my life). The most efficient answer is not always the best. In this case, it's preferable that the code be clear and concise (once the programmer understands the concept, and I got the impression that the original question may have been asked by a fairly new programmer). The trade-off is between having to walk the string character by character and compare each one (costly in and of itself) or have the system return the same results without iterating.

You'll find if you read anything I've posted here or on the web that I really get into 'different' algorythms to solve standard problems. I typically know the direction most programmers take given a standard problem like this one (because that's the first direction that comes to my mind as well), so I take that information and try to find a more elegant solution. It's a hobby of mine, and my 'weird' solutions have at time improved program performance by 10 or 100 fold. It's a good hobby, but I do admit that sometimes my elegant solution is not scalable, or is nice in theory but doesn't cut it in the real world. That's ok, if it gets me (and other programmers) to think in different ways then we've all learned something.

Thanks!

Ned

Member Avatar for iamthwee

I agree with that! It's a nice simple solution and would be tolerable if the string is short, like you have originally said.

I've only just really begun GUI coding, I mainly focus on code that is written. I enjoy using OOP and all it's advantages (inheritance etc) I am also a fan of data structures, i.e using stacks binaray trees and queues.

Like you I have an affinity for all languages, and I also enjoy working with 3d models graphics and rendering.

commented: Open minded programmer - Proud to know the developer community hasn't become jaded! +1

Ah, GUI coding. I'd almost say that you took a similar path to mine. I was a senior everything late in my mainframe days, so when I got to program I typically got to work on the fun stuff (for me, the backend code). I was thrown into .net land by a calculated job move specifically designed to force me to learn .net, and I found myself doing... GUI's.

While many brilliant programmers stay on GUI their entire career, Presentation Layer coding is typically reserved to junior staff. I know, there are some really complex GUI's out there (and I've written quite a few), but to get to some really hardcore programming, you have to push for business layer coding. How to get there? Code some really bad-a$$ GUI's, blow their socks off, and dig in to the meat of the framework as much as you can get away with. If the business layer code is available to you (in many shops it's not) then get a copy of it and tear it apart. Don't get yourself in trouble here - many shops have rules against removing code from the building (or even viewing it if you're not at the right 'level'), so be up front about what you're doing. Your employer will recognize your ambition (if they have a clue) and support it.

I look forward to trading theories with you - this is how I learn.

Ned

Thank you all for contributing to this post, it's such a beneficial and great discussion, I need years of practice to be like you ..
I hope that day would come..
I have another question which is about a "Word Counter "program,
when I count the words, I should count the spaces there.. but still after trimming string its not working as intended!
I need a function I can add so that even a group of spaces can be counted as one..
thanks alot in advance : )

Here's how I do this (and we all have to do this alot)

strText = "My Text To Count Words"  ' (LTRIMmed and RTRIMmed)
dim aryWords() as string = strText.split(" ")
dim WordCount = aryWords.length + 1 ' Arrays are zero based.

What we just did was to create an array split by Spaces (" "). The number of elements in the array is the number of words in your sentence. This is similar in concept to feeding a Comma Seperated Value table to Excel. In our case, the comma is a Space. We don't generally care about puntuation, but a hanging period (with a space before it) or a hanging comma (spaces before and after) with throw our count off. To be sure you could convert all puntuation to empty spaces ("") with a replace or a REGEX prior to the strText.split(" ") Hope that helps!

i think i still have a problem, can you show me how to do it using console application (vb.net)?

i think i still have a problem, can you show me how to do it using console application (vb.net)?

Once you have the string from your console in a variable, you can pretty much use the code I provided as written. Provide what code you've got, along with a description of the problem you face and we'll see what needs to be done next.

Ned

i didn't know how to post it ,,
but here the beginning of it:
My problem is how can i use Case statement to tell the program to group of spaces as one space, so I can count words correctly?
thanks :)


After reading the string:

For i As Integer = 1 To Len(trim(strtext))

Select Case Mid(Trim(strtext), i, 1)
Case " "
countword += 1
End Select


Next

Member Avatar for iamthwee

I see this as a more reliable word count:

Sub BtnEnterClick(sender As Object, e As System.EventArgs)
		
  Dim strText As String
  strText =  txtInput.Text
  strText = " " & strText 'insert a leading space
		
  Dim array As Char() = CType(strText, Char())
  'converts string to a character array
		
  Dim count As Integer
		
  count = 0

  Dim i As Integer

  For i=0 To Len(strText)-2 Step 1
    If array(i) = " " And array(i + 1) <> " " Then
       count = count + 1
    End If
  Next
  	
  txtResults.Text = count
 
End Sub

can you please explain for me what Len(strText)-2 Step 1 exactly means?

Member Avatar for iamthwee

The minus 2 is so that the array isn't searched beyound its physical dimensions.


Normally because you are not looping through the entire string, the string length -2 part would cause some other problem. However, it's ok in this particular case as you may well have discovered through testing.

thanks alot : )
but can u plz tell me how May I use Mid function beside the trimming to count words..
and if needed the Replace function?

Member Avatar for iamthwee

thanks alot : )
but can u plz tell me how May I use Mid function beside the trimming to count words..
and if needed the Replace function?

You wouldn't. That fact is using string split or mid or trim will not reliably count words if they are separated with multiple number of spaces.

"              A     sentence      here         "
"A sentence here"

Whereas my example will. (With the exception the words are separated with tabs or newlines instead)
And if that's the case you just write code another condition to take care of that.

You wouldn't. That fact is using string split or mid or trim will not reliably count words if they are separated with multiple number of spaces.

"              A     sentence      here         "
"A sentence here"

Whereas my example will. (With the exception the words are separated with tabs or newlines instead)
And if that's the case you just write code another condition to take care of that.

Good points, Mr. Iamthwee! The string.Split method does allow you to pass a string Array of 'match' values for your delimiters. While this is a simple problem begging for a simple solution (and yours is straight-forward and easy to understand) for the sake of making my method clear I'll recommend this:

IF you would find use (for scalability, for example) for the Replace / Split mechanism, pre-cleaning the input string would go a LONG way. Something along these lines:

Dim str AsString = "This  is a   Test and  has eight       words"
 
While str.IndexOf("  ") > 0 ' NOTE: Two spaces in the IndexOf operator
   str = str.Replace("  ", " ") ' NOTE: < That is TWO SPACES on the left side of the Replace
End While
 
Dim strArray As String() = str.Split(" ")
Dim WordCount As Integer = strArray.Length

Just an idea - even 'unacceptable solutions' help me to think outside of the proverbial box and come up with interesting solutions

Thanks for letting me play!

Ned

Member Avatar for iamthwee

That looks good, but you might have to use Ltrim and Rtrim if you have:-

Dim str AsString = "    This  is a   Test and  has eight       words     "

That looks good, but you might have to use Ltrim and Rtrim if you have:-

Dim str AsString = "    This  is a   Test and  has eight       words     "

And THIS is why collaborative development is so much fun! Nice catch!

Ned

You can also use something like this:

Dim str As String = "    This  is a  : Test and  has eight   ,    words     "
        Dim SplitChar As Char() = {" "c, ","c, ":"c}
        Dim ary As String() = str.Split(SplitChar, StringSplitOptions.RemoveEmptyEntries)

This code looks for space , a comma and a colon and splits the line at each of these characters. The RemoveEmptyEntries does not return an empty record, which would be the case with 2 or more spaces between words. The same would hold for :: or ::: and ,, or ,,,
For your purposes you would want

Dim SplitChar As Char() = {" "c}

all of you were very helpful : )
thanks alot ..

Hello Ned,

Just a wee note from a 64 year old newbie to VB.net.

My working memory has shrunk,
and never had I thunk,
that there are types like you
and retardud genius too
who will give of their time
to save so much of mine.

Thanx,

Ed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.