Solution For The Second Challenge


#1

This is my solution for the second challenge, and it works.

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

int main (int argc, const char * argv[])
{
@autoreleasepool {
NSString *propernames = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
NSString *words = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];

    NSArray *names = [propernames componentsSeparatedByString:@"\n"];
    NSArray *letters = [words componentsSeparatedByString:@"\n"];
    
    for (NSString *n in names)
    {
        for (NSString *l in letters)
        {
            if ([n isEqual:l]) {
                NSLog(@"%@", n);
            }
        }
    }
    
}
return 0;

}[/code]


#2

I do not think this works. All you get is a list of all the proper names because all proper names are also in the words file.


#3

Hey,

This is, what I came up with.
I wanted to add a counter to show the id of the objects of both arrays. Is there another, more elegant way, than mine?

[code] // Find common proper names (/user/share/dic/propernames) that are also regular words (/usr/share/dict/words)

// PROPER NAMES
// Read in a file as a huge string (ignoring the possibility of an error)
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
// Break it into an array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];

// WORDS
// Read in a file as a huge string
NSString *wordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
// Break into an array of strings
NSArray *words = [wordsString componentsSeparatedByString:@"\n"];

// Nested for-loops with counters

long wordCount = 0;
long nameCount = 0;

for (NSString *w in words) {
    
    for (NSString *n in names) {
        if ([n caseInsensitiveCompare:w] == NSOrderedSame) {
            NSLog(@"The following string is found in both lists: %@ (name #%lu) / %@ (word #%lu)", n, nameCount, w, wordCount);
            nameCount++;
            
        }
    }
    wordCount++;
    
}

[/code]