Challenge 2 Solution - checking case


#1

This is a rather long winded way of doing it. After I posted this I noticed a more sleek solution{ Challenge 2 Solution
Postby krskrft » Sat Apr 12, 2014 12:23 pm] posted

[code]//
// main.m
// findCommonNames
//
// Created by Richard Pereira1 on 9/24/14.
// Copyright © 2014 BIg Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
// Read the propernames file as a string (no error checking at this point)
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];

    // Break the string into an array of proper names
    NSArray *properNames = [nameString componentsSeparatedByString:@"\n"];
    
    // Read the words file as a string (no error checking at this point)
    NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    
    // Break the string into an array of proper names
    NSArray *regularWords = [wordString componentsSeparatedByString:@"\n"];
    
    //Loop through the array of regular words
    for (NSString *w in regularWords) {
        if ([w length] > 0) { // The check below for isUpperCase was giving me an out of range error so I added the length check
            
            //Is this an upper case word? If so it is a proper name in the words file. Ignore it
            BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[w characterAtIndex:0]];
            
            if (isUppercase) {
                // Ignore regular words already identified as proper names by the fact that they are uppercase
                continue;
            }
            
            //Loop through the proper names array looking for the regular word ignoring case
            for (NSString *pn in properNames) {
                NSString *pnLower = [pn lowercaseString]; //Lower case version of proper name
                
                // is this lower case word equal to the lower case proper name?
                if ([w isEqualToString:pnLower]) {
                    NSLog(@"Word : %@  is equal to  Name : %@", w, pn);
                }
            }
        }
    
    }
    
}
return 0;

}[/code]