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.

name the text box as txtStr
and the command button as cmdVerify
Then add the following code.
In the following general declaration, we use :-
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 :-
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.
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 :-
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)
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.
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.
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.
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.
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
This post has been edited by Xenon: 28 October 2005 - 08:51 AM






MultiQuote




|