Challenge 2 Solution

[code] //Declare globals
NSMutableArray *lowerCaseWords = [[NSMutableArray alloc]init];
NSMutableArray *capWords = [[NSMutableArray alloc]init];
//Get file into array
NSString *wordsFile = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
NSArray *wordsArray = [wordsFile componentsSeparatedByString:@"\n"];

    //Seperate words starting with capital letters & lower case into 2 arrays
    for (NSString *n in wordsArray){
        NSCharacterSet *capsList = [NSCharacterSet uppercaseLetterCharacterSet];
        NSRange capMatches = [n rangeOfCharacterFromSet];
        if (capMatches.location !=NSNotFound) {
            [capWords addObject:n];
           [lowerCaseWords addObject:n];
    //Quick sanity check to make sure the arrays have something in them
    NSUInteger capCount = [capWords count];
    NSUInteger lowerCount = [lowerCaseWords count];
    NSLog(@"Total words in Caps array %lu",capCount);
    NSLog(@"Total words in LowerCase array %lu",lowerCount);
    //compare the 2 arrays and see if there are any matches displaying them as we go
    int y=0;
    for (NSString *lC in lowerCaseWords){
        for (int x=0;x<capCount;x++){
            NSString *pN = capWords[x];
            if ([pN caseInsensitiveCompare:lC]==NSOrderedSame){
                               NSLog(@"%d %@, %@",x,lC,pN);
    //Report total matches
    NSLog(@"Total Matches %d",y);
return 0;



Here’s my solution.I chose to use just the words file and split the result into 2 arrays one with words starting with caps and one with the rest, then compare the 2 for matches. It’s probably not the most effecient way but it seems to work.

Anyone know what the correct number of matches is? I got 1515.

It should be 293.

By comparing the 2 different files rather than comparing the word file broken into words starting with caps and lowercases I get 294.

never stop after I push the running button.