7

I'm writing code to determine whether a password contains enough punctuation characters.

How do I count the number of occurrences of any characters from a set?

Something along these lines:

private const string nonAlphaNumericCharSet = "#*!?£$+-^<>[]~()&";
...
public static bool PasswordMeetsStrengthRequirements(string password)
{
    return password.Length >= MINIMUM_PASSWORD_LENGTH && password.NumberOfOccurences(nonAlphaNumericCharSet.ToCharArray()) >= MINIMUM_NONALPHANUMERIC_CHARS;
}

Bonus points for an elegant linq solution.

5
  • 3
    How do you give out bonus points? Commented May 22, 2013 at 9:56
  • @Oliver: I guess Black Knight will be awarding a bounty (after the 24 hour wait period). Commented May 22, 2013 at 9:57
  • I was thinking about awarding upvotes. And of course my own personal gratitude ;). However, if people think an answer deserves a bounty then I'll award one. Commented May 22, 2013 at 9:58
  • stackoverflow.com/a/12899943/706456 Commented May 22, 2013 at 10:00
  • Maybe this will help stackoverflow.com/questions/11569951/… Commented May 22, 2013 at 10:07

4 Answers 4

17

How do I count the number of occurences of any characters from a set?

var count = password.Count(nonAlphaNumericCharSet.Contains);
Sign up to request clarification or add additional context in comments.

4 Comments

+1, slick ..........., for those like me, its simpler form would be: var temp = password.Count(r=> nonAlphaNumericCharSet.Contains(r));
Thanks for the explanation. I was just wondering what the simpler form would be.
@Carra, yes by just 6 minutes.
That and it's also more efficient :)
1

you can count like this

int count = "he!l!l!o".Split('!').Length - 1;

it will return 3.

Using linq

int count="he!l!l!o".Count(x => x == '!');

Comments

1

Here's an example:

private const string nonAlphaNumericCharSet = "#*!?£$+-^<>[]~()&";

public static bool PasswordMeetsStrengthRequirements(string password)
{
    return password.Count(x => nonAlphaNumericCharSet.Contains(x)) > 2 && password.Length > 1;
}

public static void Main()
{
    PasswordMeetsStrengthRequirements("Test").Dump();
    PasswordMeetsStrengthRequirements("Test#").Dump();
    PasswordMeetsStrengthRequirements("(Test#").Dump();
    PasswordMeetsStrengthRequirements("(Te[st#").Dump();
}

Comments

1

what about a RegExp

Regex rgx = new Regex(@"^(?=.*(\W.*){4,}).{8,}$", RegexOptions.Compiled);
bool validPassword = rgx.IsMatch(password);

4=min not word/digit char

8= min password leght

Linq may be considered elegant (it isn't IMHO) but at which performance cost?

------------Update after comment---------------

if you want to match a subset of chars you have to replace \W with []

[]= range of chars

some chars have to be escaped with \

in your case: [#\*!\?£\$\+-\^\<\>\[\]~\(\)&]

there you can find a regular expression cheat sheet

2 Comments

Thanks. Regexs are powerful but I'm not very good at them. It's important in this case however, to only match punctuation from a predefined set, not just all punctuation. How would I do that with a regex?
@BlackKnight just updated my answer. I'm not good at regular expression either. I find very usefull this cheatsheet: github.com/shadowbq/Cheat-Sheets/blob/master/regex/… i have printed it and it is always near my desk!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.