Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,562 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,904 people online right now. Registration is fast and FREE... Join Now!




Number of times the letter occurs

 
Reply to this topicStart new topic

Number of times the letter occurs

powellm
9 Apr, 2008 - 11:05 AM
Post #1

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 20

So far here is my code

Dim strText As String
Dim strLetter As String
Dim intNumOccurences As Integer
Dim intSearchPos As Integer
Dim intLetterPos As Integer

strText = txtText.Text
strLetter = txtLetter.Text
intNumOccurences = 0
intSearchPos = 1

Do
intLetterPos = InStr(1, strText, strLetter)
If intLetterPos > 0 Then
intNumOccurences = intNumOccurences + 1
intSearchPos = intLetterPos + 1
End If
Loop While intSearchPos <= Len(strText) And intLetterPos <> 0

lblTimes.Caption = intNumOccurences

When ran in VB it gives me an error for intNumOccurences in the program

I need to know how many times a certain letter occurs in a word entered in a text box?

User is offlineProfile CardPM
+Quote Post

Shouvik
RE: Number Of Times The Letter Occurs
10 Apr, 2008 - 10:10 AM
Post #2

New D.I.C Head
*

Joined: 7 Apr, 2008
Posts: 6


My Contributions
ok.....i didn't look into your existing code. giving you a sample snippet.
try the following code. hope this helps.

Inputs are --> txtword (for the source word/string)
txtletter (for inputting the letter/character to be searched into
source word/string)
CODE

Dim i As Integer, tot As Integer
Dim cur_letter As String

tot = 0
For i = 1 To Len(Trim(txtword.Text)) Step 1
    cur_letter = Mid(Trim(txtword.Text), i, 1)
    If UCase(cur_letter) = UCase(Trim(txtletter.Text)) Then
        tot = tot + 1
    End If
Next i

MsgBox "The letter -->" & txtletter.Text & " occurs " & tot & " time(s)."


regards
Shouvik
User is offlineProfile CardPM
+Quote Post

powellm
RE: Number Of Times The Letter Occurs
10 Apr, 2008 - 10:50 AM
Post #3

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 20

can i get another approach to fixing the code [size=3]
User is offlineProfile CardPM
+Quote Post

Shouvik
RE: Number Of Times The Letter Occurs
12 Apr, 2008 - 12:49 AM
Post #4

New D.I.C Head
*

Joined: 7 Apr, 2008
Posts: 6


My Contributions
what's wrong with that code?
btw, what did you mean by [size=3]?

This post has been edited by Shouvik: 12 Apr, 2008 - 12:50 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:39PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month