My approach to Challenge 2


#1

[code] NSString *nameString1=[NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];

    NSString *nameString2=[NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                   encoding:NSUTF8StringEncoding
                                                      error:NULL];
    
    NSArray *properNames=[nameString1 componentsSeparatedByString:@"\n"];
    NSArray *commonNames=[nameString2 componentsSeparatedByString:@"\n"];
    
    
    for(NSString *n in commonNames)
    {
        
        
        for(NSString *l in properNames)
        {
            
            if([n isCaseInsensitiveLike:l])
            {
            
                NSLog(@"The Common Name: %@, matches this Proper Name:%@",n,l);
               
            }
        }
        
    }[/code]

I am pulling the common names, and putting them into an array called commonNames and doing the same with proper names and extracting them into properNames array.
Then I have a for loop which picks the first common name and tries to match looping through the properNames array. If it finds a match it traces it out in the console. If not it continues to then pick the second common name from the commonNames array and loops through the properName array and keeps repeating itself…

My output console shows:
2011-11-16 14:17:59.126 Learning1[1537:707] The Common Name: barney, matches this Proper Name:Barney
2011-11-16 14:17:59.573 Learning1[1537:707] The Common Name: Barrett, matches this Proper Name:Barrett
2011-11-16 14:17:59.669 Learning1[1537:707] The Common Name: Barrio, matches this Proper Name:Barrio
2011-11-16 14:17:59.670 Learning1[1537:707] The Common Name: barrio, matches this Proper Name:Barrio
2011-11-16 14:17:59.706 Learning1[1537:707] The Common Name: [color=#FF4000]Barry[/color], matches this Proper Name:[color=#FF4000]Barry[/color]
2011-11-16 14:17:59.707 Learning1[1537:707] The Common Name: [color=#FF4000]barry[/color], matches this Proper Name:[color=#FF4000]Barry[/color]

For some reason in my common name list/array I have “barry” and I also have “Barry” the proper name. I thought the “/usr/share/dict/words” would not have any proper names in it as I read through the book challenge. But for some reason the console does find the proper name “Barry” in the common word list !

Please correct me if I am doing something wrong.


#2

Take a look at the other threads in this chapter. The word list now contains the proper nouns and threw all of us off at first!


#3

Thanks, yehh!!