Counting letters in a paragraph

Any idea of how I would be able to use it to count if all the letters

Page 1 of 1

12 Replies - 3237 Views - Last Post: 13 October 2007 - 12:37 AM Rate Topic: -----

#1 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Counting letters in a paragraph

Post icon  Posted 11 October 2007 - 12:45 AM

A curious thought, is there any code that will enable me to count all the letters of the alphabet in a status strip as I type them in the textbox? (This also means ignoring punctuation and numbers respectively)

Example:
Unique alphabetic letters count: 5 typing.. abcde1234567..<<:"{}

When all alphabetic letters are used, the count stops at 26.

Unique alphabetic letters count:26 typing.. abcdefghijklmnopqrstuvwxyz1234567..<<:"{}

This I found quite challenging, perhaps a few advanced users may be able to figure this out.

This post has been edited by strider2: 11 October 2007 - 12:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Counting letters in a paragraph

#2 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Counting letters in a paragraph

Posted 11 October 2007 - 02:31 AM

Well, let's see.

First you would create a counter variable for each letter and set it to 0.

Inside of a Text_Changed event use String.char(string.length-1) to grab the last character typed and store it in a variable. Then use either a Select Case statement or multiple if/then/elseif statements to determine if its a letter of the alphabet.

Use an if/then/else statement against the counter value for that letter to see if its been found already. If not, then add one to the counter. For the total, add all the counters together.

I would try to write out the code, but I've been coding for about 12 hours and my brain has pretty much shut down for the time being. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 11 October 2007 - 03:14 AM

Hm, I've tried doing that but I still cannot get it.
I have been messing around with this letter counting business for days.



Thank you for trying though.
Was This Post Helpful? 0
  • +
  • -

#4 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Counting letters in a paragraph

Posted 11 October 2007 - 05:56 AM

I don't know if I got what you want... I'm assuming your textbox is named "TextBox1" and there's a label named "Label1". Then use the following in the TextChanged event.
		' store text in a character array
		Dim caText() As Char = TextBox1.Text.ToLower().ToCharArray()
		' sample character list
		' don't have to be "a" to "z" in that order. So long as each character
		' is unique in the string, it's fine.
		Dim sLetters As String = "abcdefghijklmnopqrstuvwxyz"
		' counter array
		Dim iaCounter(sLetters.Length - 1) As Int32
		Dim i As Int32 = 0
		Dim iBuffer As Int32 = 0

		' initialise counter array
		For i = 0 To iaCounter.Length - 1
			iaCounter(i) = 0
		Next

		For i = 0 To caText.Length - 1
			' if "a" is found, then index is 0
			' if "e" is found, then index is 4
			' if a character isn't found, then index is -1
			' Index thus found also matches with corresponding
			' counter entry in iaCounter array
			iBuffer = sLetters.IndexOf(caText(i))
			If (iBuffer >= 0) Then
				iaCounter(iBuffer) = iaCounter(iBuffer) + 1
			End If
		Next

		' counting unique alphabets
		iBuffer = 0
		For i = 0 To iaCounter.Length - 1
			If (iaCounter(i) > 0) Then
				iBuffer = iBuffer + 1
			End If
		Next

		Label1.Text = String.Format("Unique alphabetic letters count: {0}", iBuffer)



The idea is to use a sample string containing all the valid characters. Then create an integer array of the corresponding length for counting purposes.

I didn't want to hardcode 26. This way you can add "2357!?" into the sample string and can then check for 26 alphabets, 4 digits and 2 symbols, and the rest of the code still works.

A nonnegative return value from IndexOf() function means the letter being compared is a valid character that you want. The return value also happens to be an index into the counter array.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 11 October 2007 - 01:53 PM

You've answered the problem correctly.
Good work my friend! :)

This post has been edited by strider2: 11 October 2007 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 11 October 2007 - 11:27 PM

Okay, here's a moderate difficulty challenge.

Say you typed abcdefg.

Assuming you want the remaining letters to be placed in a label as you are typing in the textbox. For instance
typing.. abcdefghijk

Label: lmnopqrstuvwxyz

As you continue to use all the letters of the alphabet, the letters from the label are removed.
If you remove the letters from the textbox, they will go back in their proper order to the label.

hehe, good luck on this one.
Was This Post Helpful? 0
  • +
  • -

#7 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Counting letters in a paragraph

Posted 12 October 2007 - 12:01 AM

Simple enough if you're looking at the code from before, you just assign the string array to a label, and each time one of those is typed, you remove it from the array instead of adding to the counter.

Just wondering, are you trying these yourself or do you just feel like quizzing us?

This post has been edited by aceofspades686: 12 October 2007 - 12:02 AM

Was This Post Helpful? 0
  • +
  • -

#8 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 12 October 2007 - 12:17 AM

I am doing both actually, it's good for exercising skills and giving other users practice as well.
If you guys want me to stop quizzing I will. I just thought it would be kind of interesting, a community thing.

This post has been edited by strider2: 12 October 2007 - 12:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Counting letters in a paragraph

Posted 12 October 2007 - 01:15 AM

View Poststrider2, on 12 Oct, 2007 - 02:17 AM, said:

I am doing both actually, it's good for exercising skills and giving other users practice as well.
If you guys want me to stop quizzing I will. I just thought it would be kind of interesting, a community thing.


Nah, there's nothing wrong with it, but you have to admit that it seems a bit suspicious that all your posts have been in this thread because that makes it seem as though you're asking about this code as a sort of way to get others to do work for you. I'm honestly fine with it either way (in this case anyway, even though I've been writing a lot of code lately and too tired to think too hard on it, they're fun little quizzes) but the community itself has a "we're not going to do your work for you" policy, so I figured it wouldn't hurt to ask and make sure that wasn't the case.

So don't mind me, continue on. Sorry if I seemed a bit standoffish in my last post.

This post has been edited by aceofspades686: 12 October 2007 - 02:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 12 October 2007 - 01:56 AM

No worries, It's always good to make sure.

I Noticed these forums are active a lot, so I figured it would be fine messing around with code and chat all day to pass the time.

Anyway, the quiz shall resume.

The program must remove/add alphabet letters from the label as they are typed or removed in the textbox. ie.
label:abcdefghijklmnopqrstuvwxyz

Typing letter into textbox.text: Abc

label: defghijklmnopqrstuvwxyz <--label contains unused letters after user enters "Abc".

label:defghijklmnopqrstuvwxyz
textbox:A <--backspaced b and c from textbox.
Note that B and C reappear as unused letters as they were backspaced from the textbox.
label:bcdefghijklmnopqrstuvwxyz

I'll continue to have a look to see if it will work if I mess around with the loop statements (For i = 0 To caText.Length - 1) especially.
Was This Post Helpful? 0
  • +
  • -

#11 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Counting letters in a paragraph

Posted 12 October 2007 - 04:50 AM

Quote

but the community itself has a "we're not going to do your work for you" policy

Oh yes, after I posted my code, I actually got a bad feeling that the higher powers of DIC are gonna nuke me or something...

Well, if it's just a quiz, then try this
		' store text in a character array
		Dim caText() As Char = TextBox1.Text.ToLower().ToCharArray()
		' sample character list
		' don't have to be "a" to "z" in that order. So long as each character
		' is unique in the string, it's fine.
		Dim sLetters As String = "abcdefghijklmnopqrstuvwxyz"
		' counter array
		Dim iaCounter(sLetters.Length - 1) As Int32
		Dim i As Int32 = 0
		Dim iBuffer As Int32 = 0

		' initialise counter array
		For i = 0 To iaCounter.Length - 1
			iaCounter(i) = 0
		Next

		For i = 0 To caText.Length - 1
			' if "a" is found, then index is 0
			' if "e" is found, then index is 4
			' if a character isn't found, then index is -1
			' Index thus found also matches with corresponding
			' counter entry in iaCounter array
			iBuffer = sLetters.IndexOf(caText(i))
			If (iBuffer >= 0) Then
				iaCounter(iBuffer) = iaCounter(iBuffer) + 1
			End If
		Next

		Label2.Text = String.Empty
		' counting unique alphabets
		iBuffer = 0
		For i = 0 To iaCounter.Length - 1
			If (iaCounter(i) > 0) Then
				iBuffer = iBuffer + 1
			Else
				Label2.Text += sLetters(i)
			End If
		Next

		Label1.Text = String.Format("Unique alphabetic letters count: {0}", iBuffer)


I'm assuming there's a label named "Label2" somewhere. So, here's my quiz. Where are the modifications, and explain why/how they work. :D
Was This Post Helpful? 0
  • +
  • -

#12 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 12 October 2007 - 10:25 AM

Ah, I see there are only a few changes to be made.
Okay here goes. I commented where the modifications are, correct me if I am wrong.


---------------------------------------------------------------------
Label2.Text = String.Empty
'empties the string of the alphabetic letters as they are typed.
------------------------------------------------------------------------

-----------------------------------------------------------------------------
Else "<-- I see an else statement
Label2.Text += sLetters(i)
'Adds and equates the letters by
'their number but displays the letters themselves by their length in 'the label. Only if they are present. Of course, if they were 'removed from the textbox, the string will add the unused alphabetic letters back to the label.
--------------------------------------------------------------------------

:) Nice one btw!

This post has been edited by strider2: 12 October 2007 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#13 strider2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-October 07

Re: Counting letters in a paragraph

Posted 13 October 2007 - 12:37 AM

Should any of you have a programming quiz to give us. Just make up a quiz and give it a difficulty. Doesn't have to be spontaneous, (it can be).

After which one of us will come up with the code and if the code works then the coder will get a point. Whoever gets the most points by the end of next week wins the title of (Skilled programmer). Not picking favorites, but it's a memorable title.

This is only a game, provided the forum policy suggests that "We will not do your work for you". The code displayed is for the sake of the game only (to make sure it works, so we can give a score to the player).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1