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

Join 108,956 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,545 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Visual Basic 6 -Determining Each Character in VB

 
Reply to this topicStart new topic

> Visual Basic 6 -Determining Each Character in VB, Very Handy

Xenon
Group Icon



post 26 Oct, 2005 - 02:24 AM
Post #1


Examining Characters in a string.

Note : - i ve explained the code Block by block, but when you try it in VB keep it in the SAME ORDER that follows in this tutorial.

The following application verifies the characters in a given string and returns details about the string.

First of all make the following interface. i havent changed the name of the form, so the form name is "form1", but only the caption.

IPB Image

name the text box as txtStr
and the command button as cmdVerify

Then add the following code.

In the following general declaration, we use :-
CODE


Option Explicit
Dim strl As String
Dim chrl As String * 1 ‘ Specifies the length to one digit.
Dim ctr As Integer

Dim letters As Integer
Dim UpCase As Integer
Dim LoCase As Integer
Dim digits As Integer
Dim blanks As Integer
Dim others As Integer


where the variable :-
strl – stores the string passed from the textbox.
chrl – stores extracted character from the string.
ctr – the position number from which the character is
extracted.

Letters (a, e, t, y, f, s etc) – stores the number of letters counted in string.

UpCase – Stores the number of Uppercase letters in string.
LoCase – stores the number of Lowercase letters in string.
digits ( 0 to 9)– stores the number of digits counted in string.

blanks (“ “)– stores the number of blank spaces in string.

others (%#^&*<>?: =)– stores the number of special characters.

Now add this code in the click event of the command button named cmdVerify :-

CODE


Private Sub cmdVerify_Click()

‘ß This code clears the form screen of earlier prints so that you can start printing from the first line.
Form1.Cls  
‘ß This code refreshes the form.
Form1.Refresh

‘ Now setting the initial values of all variables to zero.

letters = 0
digits = 0
blanks = 0
others = 0

‘ Passing the textbox string to the variable.
strl = txtStr.Text


Now carefully look at the code that is preceding , ill explain it line by line.
CODE

For ctr = 1 To Len(strl)
chrl = Mid(strl, ctr, 1)


In the above code,
the first line is a part of a for loop, that goes on from one till the length of the string is achieved.

The second line then uses the MID function to extract the particular character from the sting at a time. Now the general syntax of the MID function is,

Mid(“String”, Start Pos,[Length])

Where “String” is the String on which you are going to perform the MID function, Start Pos is the position from which the first character is to be extracted, and length is the number of characters to be extracted from the string preceding from the Start Pos, including the Start Pos Character.

Now coming to the next par of the code :-
CODE

If (UCase(chrl) >= "A" And UCase(chrl) <= "Z") Then
letters = letters + 1

The above 2 lines of code can be described as follows,

We take the extracted character ‘chrl’ and compare it to all the alphabets from A to Z, keeping the character ‘ignore-case’
If IT IS a letter, then the value of the variable ‘letter’ is increased by one in the next line (letters = letters + 1)

In the Next 5 lines of code, it determines whether it is an uppercase letter or a lowercase one,(only if it is a letter)
CODE

If ((chrl) >= "A" And (chrl) <= "Z") Then
UpCase = UpCase + 1
ElseIf ((chrl) >= "a" And (chrl) <= "z") Then
LoCase = LoCase + 1
End If


This part of the code compares the character to be digit from 0 to 9 or not. If it is a digit increase the value of the variable ‘digit’ by one.
CODE

ElseIf (chrl >= "0" And chrl <= "9") Then
digits = digits + 1


This part of the code compares the character to be a blank space or not. If it is a blank space increase the value of the variable ‘blanks’ by one.
CODE

ElseIf (chrl = " ") Then
blanks = blanks + 1


Now the only character that can exist in a visible string are the special characters, if the character fails all the above tests, then it is a special character.
CODE

Else
others = others + 1
End If
‘End the FOR LOOP.
Next ctr


Now we print all the details of the variables onto the form in a nice manner.
CODE

Print " -------------------------------------------------------"
Print " Details about the string you have entered "
Print " ---------------------------------------------------------"
Print " Number of letters is =  " & letters
Print " Number of Lower case letters is =  " & LoCase
Print " Number of Uppercase letters is  =  " & UpCase
Print " ----------------------------------------------------------"
Print " Number of digits is = " & digits
Print " Number of blank spaces is = " & blanks
Print " Number of special characters is = " & others
Print " -------------------------------------------------------------"
Print " Total Length of String is "; Len(txtStr.Text); " characters "
Print " -------------------------------------------------------------"
End Sub


of course, the END SUB will automatically be generated when you double click the command button in design time, hence it wont be missed out.

Hope this Tutorial is helpful smile.gif

This post has been edited by Xenon: 28 Oct, 2005 - 08:51 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

oXiDe
*



post 22 Nov, 2007 - 03:12 PM
Post #2
hi i think you forgot to do LoCase = 0 & UpCase = 0 at the beginning because if u keep pressing Verify String the # of uppercase & lowercase keeps going up :l . still this tutorial pwns :D
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/4/08 09:24PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month