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

Join 118,311 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,678 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!



C# - Reading each character of a string

 
Reply to this topicStart new topic

> C# - Reading each character of a string

hash_code21
*



post 17 Oct, 2005 - 07:14 AM
Post #1


A while ago I had to write a quick function that determined the number of upper case letters, lower case letters, numbers, and symbols in a password field that appeared on a C#.NET web application. With that information we determined which characters were illegal and how string the password was.

I am going to leave out the logic that we used to determine how striong the password was because it was a criteria that we came up with for our customers. Below you will see how I chose to enumerate through each character of the string and determine what exact character it was without comparing it as a System.String to EVERY character out there!

LOGIC - First, I read the string into a local variable. Next, I create a CharEnumerator to read each character of the string with the MoveNext() function. During the enumeration the character is converted to it's Int32 equivalent as it will appear in ASCII decimal form. That newly converted value is compared using if statements to determine whether it fits the values for ucaseletter, lcaseletters, numbers, or symbols.

CODE
string pw = txtPassword.Text;
     int convertedPw;
     int counter = 0;
     int uLetters = 0;
     int lLetters = 0;
     int numbers = 0;
     int symbols = 0;

     lblDetails.Text = "";
     lblResults.Text = "";

     // #### >BEGIN< PROCESSING EACH CHARACTER ####
     CharEnumerator charEnum = pw.GetEnumerator();
     while (charEnum.MoveNext())
     {
               convertedPw = Convert.ToInt32(pw[counter]);
   
   if ((convertedPw >= 65) && (convertedPw <= 90))
   {
       uLetters++;
   }  
   
   if ((convertedPw >= 97) && (convertedPw <= 122))
   {
       lLetters++;
   }

   if ((convertedPw >= 48) && (convertedPw <= 57))
   {
       numbers++;
   }

   if ((convertedPw >= 33) && (convertedPw <= 47))
   {
       symbols++;
   }

   if ((convertedPw >= 58) && (convertedPw <= 64))
   {
       symbols++;
   }

   if ((convertedPw >= 91) && (convertedPw <= 96))
   {
       symbols++;
   }

   if ((convertedPw >= 123) && (convertedPw <= 126))
   {
       symbols++;
   }

   counter++;
     }
     // #### >END< PROCESSING EACH CHARACTER ####

     lblDetails.Text += "Upper Case Letters: " + uLetters.ToString() + "\n";
     lblDetails.Text += "Lower Case Letters: " + lLetters.ToString() + "\n";
     lblDetails.Text += "Numbers: " + numbers.ToString() + "\n";
     lblDetails.Text += "Symbols: " + symbols.ToString() + "\n";


As you can see, I included a textbox for them to enter the password, a button that contains the above code in it's click event handler, and a label control called lblDetails.

I hope this is a tutorial worthy of posting on here. It is really simple and very handy!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Amadeus
Group Icon



post 17 Oct, 2005 - 03:05 PM
Post #2
That is a good tutorial, and implements some good practices. I would point out, however, that a program may be better optmized using the .IsDigit(), .IsLetter, and .IsSymbol methods from the Char object. As predefined methods, they will run faster, and eliminate any possible exclusions, especially among the symbols.
Go to the top of the page
+Quote Post

santa.clara
*



post 11 Oct, 2007 - 07:30 AM
Post #3
Pleas note that the following 2 strategies give different results !!
for(int ix=0;ix<pw.Length;ix++)
{
if(char.IsDigit(pw,ix))digits++;
else if(char.IsUpper(pw,ix))upper++;
else if(char.IsLower(pw,ix))lower++;
else if((char.IsSymbol(pw,ix))
| (char.IsControl(pw, ix))
| (char.IsPunctuation(pw, ix))
| (char.IsWhiteSpace(pw, ix))) theRest++;
}
// #### >BEGIN< PROCESSING EACH CHARACTER ####
foreach(char c in pw)
{
Int16 code=Convert.ToInt16©;
if((code>=33 & code <=47) | (code>=58 & code <=64) | (code>=91 & code <=96) | (code>=123 & code <=126))symbols++;
else if((code>=65 & code <=90) )uLetters++;
else if((code>=97 & code <=122) )lLetters++;
else if((code>=48 & code <=57) )numbers++;
}
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/10/08 12:05PM

Live C# Help!

C# Tutorials

Reference Sheets

C# 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