8 Replies - 476 Views - Last Post: 10 July 2012 - 10:19 PM Rate Topic: -----

#1 kaittieboo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

What method should I use for counting letters?

Posted 09 July 2012 - 04:20 PM

I'm trying to make it so when you input hello you can also input a letter and it will tell you how many times your letter is in the string.
Here's the form: http://imgur.com/wU1ml
Can anyone tell me what I should use for this? Like what loop or substring? Please and thank you!
This is what I have, it's not good and it doesn't work.
Public Class Form1
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim strInput As String
        Dim strSearch As String
        Dim cnt As Integer = 0
        strInput = Me.txtEntx.Text
        strSearch = Me.txtSer.Text

        For Each Val(strInput) As Char In Val(strSearch)
            If Val(strInput) = 
        Next
    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: What method should I use for counting letters?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: What method should I use for counting letters?

Posted 09 July 2012 - 04:36 PM

Well of course it doesn't work, you stopped typing in the middle of line 10. You're on the right track - keep going. You've already taken the user 'search for' string as strSearch. So check if the 1 letter is equal to that.

In short I'm saying, "You can't just stop and give up and throw you hands in the air. You have to experiment a bit. Trial and error. To grow beyond your comfort area." You've got about 60 seconds of code here then you just stopped without trying a half a dozen different things. Push yourself a bit. You really are on the right track. Don't be afraid to keep going.

You should also read up on how that for each should be formed.
http://msdn.microsof...y/5ebk1751.aspx
the syntax is:
for each variablename as variableType in object
for each temp as char in Val(strSearch)


Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

Was This Post Helpful? 3
  • +
  • -

#3 kaittieboo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: What method should I use for counting letters?

Posted 09 July 2012 - 07:39 PM

View PosttlhIn`toq, on 09 July 2012 - 04:36 PM, said:

Well of course it doesn't work, you stopped typing in the middle of line 10. You're on the right track - keep going. You've already taken the user 'search for' string as strSearch. So check if the 1 letter is equal to that.

Well, I fiddled with it and it still gives me an error on the first variable in the for each.
        Dim strInput As String
        Dim strSearch As String
        Dim cnt As Integer = 0

        strInput = Me.txtEntx.Text
        strSearch = Me.txtSer.Text
        cnt = lblAnswer.Text

        For Each strSearch As Char In strInput
            cnt += 1
        Next

It says "Variable 'strSearch' hides a variable in an enclosing block." I don't know what that means or how to fix it. :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: What method should I use for counting letters?

Posted 09 July 2012 - 09:28 PM

Ummm... I gave you the code you need for that line

View PosttlhIn`toq, on 09 July 2012 - 05:36 PM, said:

for each temp as char in Val(strSearch)


You REALLLY need to read the MSDN on for each until it starts making sense to you.

You don't re-use a variable you have already defined. You are defining a new variable to use within the scope of the for each block.

You already have half of a proper if check written, showing that you understand that you need to actually check if the letter in the word matches the letter you are searching for:
10 If Val(strInput) =

Then you just throw all that away and decide to count each and ever leter in the word, whether it matches or not:
10 cnt += 1

One of two things is happening here:
Either A: You are pretending to be really really thick and have no clue in the hopes that someone will do you homework for you or
B: You really are completely and utterly lost in your understanding of the most basic lessons you've learned in class so far, and are just banging on the keyboard putting together random combinations of words from your text book hoping that something starts working by voodoo.

If it's A and you're just not interested in doing your homework and hoping to trick someone else into doing it: Drop out of the course.

If it's B and you're just this lost: You very much need to go to your professor and ask for help or tutoring. Do not go another week hoping it will all magically 'snap' into place and make sense. Coding doesn't work that way. If you are lost now, next week's chapter will build on this weeks; only you don't understand this week's so you become more lost. The follow week's lesson will build on next week's and you become even more lost.

Re-read your text book. Seek out help from your professor. Go buy a good "Learn VB.NET in 30 days" type book from Amazon.com or the school/public library. Find the smartest egg-head in your class and pay him to tutor you. Do whatever you need to do to get caught up. The stuff you are struggling with here is 1st week material and you can not proceed without actually understanding it.
Was This Post Helpful? 0
  • +
  • -

#5 kaittieboo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: What method should I use for counting letters?

Posted 10 July 2012 - 09:07 PM

View PosttlhIn`toq, on 09 July 2012 - 09:28 PM, said:

really thick

We'll go with that one. It's an online class. We use an Instant Messenger to communicate with the teacher and, oh look! it's midnight and she isn't on. I just got home from work, got on my computer and shed a few tears out of pure frustration when looking at the program that I have spent hours working on. Then I see a reply from you. Of course, my hopes rise from the ashes and I look expecting the code guru to have some wisdom or a nudge in the right direction, but instead I get the same fortune cookie run around bull that I got from the tens of google searches. Thank you, really! thank you! for crushing my hopes of going into computer science since I'm a lost cause here not understanding this obviously easy material that you so readily understand. Because even though you possess such superior knowledge you cannot, for the life of you, share a bit of it with an aspiring programmer. Really, again, thank you. (that may not mean much coming from this really thick person)
Now normally I'm the type of person to finish typing this and just not post it. But I would really like you to read this, though I doubt you will.
Was This Post Helpful? 0
  • +
  • -

#6 EightyFive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-July 12

Re: What method should I use for counting letters?

Posted 10 July 2012 - 09:23 PM

It was a nudge in the right direction, all you have to do is check if the char in the loop is the same as the char you want to match.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: What method should I use for counting letters?

Posted 10 July 2012 - 09:50 PM

Start with Pencil and paper.
Create a psuedocode algorithm.

Example: A Circle
Try standing up and closing your eye, think how you who to walk in a circle.
Think what you did.
Spoiler


Now apply that technique to washing clothes.
What steps did you do?
Spoiler


Do write a PSEUDO-CODE algorithm for your problem.
Does it work on paper? If not then tweak and change it until it does.
Once you have an algorithm convert it in code.

0. Start with a console application (absolutely no GUI forms)
1. Write pseudocode algorithm as comments in the code.
2. underneath each line, write the code which achieves it.
3. Can't figure some thing out then research the documentation and resources.
4. Play and Experiment with code.
5. Learn about the debugger
Spoiler

This post has been edited by AdamSpeight2008: 10 July 2012 - 09:58 PM

Was This Post Helpful? 1
  • +
  • -

#8 kaittieboo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: What method should I use for counting letters?

Posted 10 July 2012 - 10:14 PM

View PostAdamSpeight2008, on 10 July 2012 - 09:50 PM, said:

Do write a PSEUDO-CODE algorithm for your problem.
Does it work on paper? If not then tweak and change it until it does.
Once you have an algorithm convert it in code.

This is how that works for me: http://imgur.com/Fe0Wp
I'm clueless and at this point, belligerent.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: What method should I use for counting letters?

Posted 10 July 2012 - 10:19 PM

How would write and pseudo-code algorithm to count just one particular letter in a text string?

Function CountHowManyTimes(ThisLetter As Char, InThisText As String ) As Integer
// Write your pseudo code here
End Function


This post has been edited by AdamSpeight2008: 10 July 2012 - 10:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1