Challenge 2 - Is my code inefficient?


#1

After reading through the threads here, it occurred to me that the strings in the words array I was trying to match were the lowercase equivalents of the names array. This had me stumped. I read through the dox and came up with the following code. Essentially, I figured I would transform the names first, then look for matches. As I am relatively new to this, I feel like this approach is maybe a bit inefficient or possible incorrect. It appears to work, but am curious as to some feedback for ways I could improve it.

[code]int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    //Read in the names file as a huge string, ignoring the possibility of an error
    NSString *nameString = 
    [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" 
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    //Convert the names to all lowercase so they will match the corresponding words
    NSString *alteredNameString = [nameString lowercaseString];
    
    //Break the altered names into an array of strings
    NSArray *alteredNames = [alteredNameString componentsSeparatedByString:@"\n"];
    
    //Read in a words file as a huge string, ignoring the possibility of an error
    NSString *wordString = 
    [NSString stringWithContentsOfFile: @"/usr/share/dict/words" 
                              encoding:NSUTF8StringEncoding 
                                 error:NULL];
    
    //Break words into an array of strings
    NSArray *words = [wordString componentsSeparatedByString:@"\n"];
    
    //Log some numbers of stuff for error checking
    NSLog(@"There are %lu names", [alteredNames count]);
    NSLog(@"There are %lu words", [words count]);
    
    NSUInteger counter = 0;
    
    //Go through the words array one string at a time and compare against the names
    for (NSString *n in alteredNames) {
        
        for (NSString *w in words) {
            if ([n isEqualToString:w]) {
                NSLog(@"%@ is a match", n);
                counter++;
                break;
            } 
        }
        
    }
    NSLog(@"There are %lu matches", counter);
    
    
}
return 0;

}
[/code]

Thanks in advance and I am really enjoying the book.


#2

Thats one way, but there is a a method called caseInsensitiveCompare: if you use that and test the result against NSOrderedSame you will find out if the strings are the same, in other words:

NSString *firstString = @"This is a test";
NSString *secondString = @"this is a test";
NSString *thirdString = @"this is the final test";

if ( [firstString caseInsensitiveCompare:secondString] ==  NSOrderedSame )
NSLog(@"These are the same");

if ( [secondString caseInsensitiveCompare:thirdString] !=  NSOrderedSame )
NSLog(@"These are now the same");

The reason this works is becase compare: and caseInsensitiveCompare: look to see where (alphabetically) the two NSStrings are in comparison, if you take a simpler example, with three strings, @“A”, @“B” and @“C” then say [@“A” caseInsensitiveCompare: @“B”] would return NSOrderedAscending, [@“B” caseInsensitiveCompare: @“A”] would return NSOrderedDescending, and [@“A” caseInsensitiveCompare: @“B”] would return NSOrderedSame.

I hope this helps, it was confusing for me too at first. Good luck!