954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

hello i have a problem

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 :/

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Yankie Dave
Newbie Poster
6 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

NedFrankly
Light Poster
26 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

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?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

NedFrankly
Light Poster
26 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 
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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

NedFrankly
Light Poster
26 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

NedFrankly
Light Poster
26 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

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 : )

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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!

NedFrankly
Light Poster
26 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

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

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
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

NedFrankly
Light Poster
26 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

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

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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?

vb_learner
Newbie Poster
13 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You