Chapter 15 Challenge Solution


#1

@autoreleasepool {

    // Read in a file as a huge string (ignoring the possibility of an error)
    NSString *properNameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    NSString *regularWordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    
    // Break it into an array of strings
    NSArray *properNameArray = [properNameString componentsSeparatedByString:@"\n"];
    NSArray *regularWordsArray = [regularWordsString componentsSeparatedByString:@"\n"];
            
    // Go through the both array one string at a time and compare it one string at a time
    for (NSString *regularWord in regularWordsArray) {
        for (NSString *properName in properNameArray) {
            if (NSOrderedSame == [regularWord caseInsensitiveCompare:properName]) {
                NSLog(@"Regular word is: %@", regularWord);
                NSLog(@"Proper name is: %@", properName);
            }
        }
    }
    
}

#2

This is incorrect code, you’ll see your output is matching every proper name (the words file has proper names in it as well):

2013-03-23 16:18:05.052 arraychallenge2[4015:303] Regular word is: Celeste
2013-03-23 16:18:05.053 arraychallenge2[4015:303] Proper name is: Celeste
2013-03-23 16:18:05.054 arraychallenge2[4015:303] Regular word is: celeste
2013-03-23 16:18:05.054 arraychallenge2[4015:303] Proper name is: Celeste


#3

Is this better??

[#import <Foundation/Foundation.h>

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

@autoreleasepool
{
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    NSString *regularWords = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    NSArray *words = [regularWords componentsSeparatedByString:@"\n"];
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    
    NSMutableArray *commonall = [NSMutableArray array];
    [commonall addObjectsFromArray:words];
    [commonall removeObjectsInArray:names];
    
 
    for (NSString *w in commonall)
    {
        for (NSString *n in names)
        {
            if ([w caseInsensitiveCompare:n] == NSOrderedSame)
            {
                NSLog(@"Name:%@ = word:%@", n, w);
            }
        }
    }

}
return 0;

}][/code]


#4

[quote=“callmeisaac”]Is this better??

[code]
[#import <Foundation/Foundation.h>

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

@autoreleasepool
{
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    NSString *regularWords = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    NSArray *words = [regularWords componentsSeparatedByString:@"\n"];
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    
    NSMutableArray *commonall = [NSMutableArray array];
    [commonall addObjectsFromArray:words];
    [commonall removeObjectsInArray:names];
    
 
    for (NSString *w in commonall)
    {
        for (NSString *n in names)
        {
            if ([w caseInsensitiveCompare:n] == NSOrderedSame)
            {
                NSLog(@"Name:%@ = word:%@", n, w);
            }
        }
    }

}
return 0;

}[/code][/quote]

To me, yes this is correct. Simple solution. :slight_smile:

If it’s not, guys, please add some comments. Thanks.

Mine is basically the same:

        NSString *compStringA = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                          encoding:NSUTF8StringEncoding
                                                             error:NULL];
        NSString *compStringB = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                          encoding:NSUTF8StringEncoding
                                                             error:NULL];
        
        NSArray *namesA = [compStringA componentsSeparatedByString:@"\n"];
        NSArray *namesB = [compStringB componentsSeparatedByString:@"\n"];
        
        NSMutableArray *noProperName = [NSMutableArray array];
        [noProperName addObjectsFromArray:namesB];
        [noProperName removeObjectsInArray:namesA];

        
        for (NSString *n in namesA) {
            for (NSString *m in noProperName) {
                if ([n caseInsensitiveCompare:m] == NSOrderedSame) {
                        NSLog(@"%@ is a name", n);
                        NSLog(@"%@ is a word", m);
                }
            }
        }

#5

Question from french newbie on mac programming (40 years developping applications on
main frames and on micro under Windows (sorry, not my choice)) …
I can’t find the files “usr/share/dict/propernames” and “usr/share/dict/words”.
Where are they ? In french, I can situate usr (utilisateurs), share (partagé) but ther is
no equivalent for “dict” … :blush:


#6

Hi Guys, someone further up said that the following code is wrong… I’m struggling to see why that is? I would be inclined to agree that perhaps it’s not as ‘useful’ because it checks both capitalised and non-capitalised letters but it still seems to correctly output for example;

2014-08-23 15:55:09.321 comparing namesch15[28236:303] Name:Celeste = word: Celeste
2014-08-23 15:55:09.322 comparing namesch15[28236:303] Name:Celeste = word: celeste

It correctly verifies that both the capitalised and non-capitalised versions are identical. So what is actually wrong with this?

A guy further up suggested adding;

    NSMutableArray *commonall = [NSMutableArray array];
    [commonall addObjectsFromArray:words];
    [commonall removeObjectsInArray:names];

But I’m failing to see what this additional array is actually achieving? First off, isn’t this array trying to remove objects that aren’t even present in the array?

Thanks in advance.

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

@autoreleasepool {
    //Read in a file as one huge string (ignoring errors)
    NSString *namestring = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    
    //Read in a file as one huge string - this time regular words
    NSString *regular = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    
    //break this into an array of smaller strings
    NSArray *names = [namestring componentsSeparatedByString:@"\n"];
    
    //break this into an array of smaller strings
    NSArray *reg = [regular componentsSeparatedByString:@"\n"];
    
    //cycle through array
    for(NSString *re in reg){
        for(NSString *n in names){
            if([n caseInsensitiveCompare:re] == NSOrderedSame){
                NSLog(@"Name:%@ = word: %@",n, re);
            }

return 0;
}
[/code]