Counting occurrences of capital letters and numbers in an NSString

nsstring, objective-c

Solution

You can use the `NSCharacterSet`:

NSString *password = @"aas2dASDasd1asdASDasdas32D";
int occurrenceCapital = 0;  
int occurenceNumbers = 0;
for (int i = 0; i < [password length]; i++) {
    if([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[password characterAtIndex:i]])
       occurenceCapital++;

    if([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[password characterAtIndex:i]])
       occurenceNumbers++;

}

Problem

In PHP I am using the following code... ``` $passwordCapitalLettersLength = strlen(preg_replace("![^A-Z]+!", "", $password)); $passwordNumbersLength = strlen(preg_replace("/[0-9]/", "", $password)); ``` ...to count how many times capital letters and numbers appear in the password. What is the equivalent of this in Objective C?

Original source