Page 1 of 1

C# - Reading each character of a string

#1 hash_code21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 12-October 05

Posted 17 October 2005 - 07:14 AM

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.

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!

Is This A Good Question/Topic? 1
  • +

Replies To: C# - Reading each character of a string

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Posted 17 October 2005 - 03:05 PM

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.
Was This Post Helpful? 1
  • +
  • -

#3 santa.clara  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 01-May 07

Posted 11 October 2007 - 07:30 AM

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++;
}
Was This Post Helpful? 1
  • +
  • -

#4 laserbeak43  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 134
  • Joined: 07-November 09

Posted 14 August 2010 - 04:02 PM

Cool thanks ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1