This is my solution for Challenge 2. It is not optimal, but it works! Any comments are appreciated.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Read in a file as a huge string (ignoring the possibility of an error)
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
//Break it into an array of strings
NSArray *strings = [wordString componentsSeparatedByString:@"\n"];
//Create an empty array for words
NSMutableArray *words = [NSMutableArray array];
//Create an empty array for names
NSMutableArray *names = [NSMutableArray array];
//Populate the array of words with words from the strings array
for(NSString *s in strings){
//ensure that the string is not empty
if(![s isEqual: @""]){
//check if the first character is uppercase
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
//if it is a lowercase letter, add it to the words array
if(!isUppercase){
[words addObject:s];
}
//else, the first letter is uppercase, add it to the names array
else{
[names addObject:s];
}
}
}
//Iterate through the words array and compare each word to each name in the names array
//NSLog matches
for(NSString *s in words){
for(NSString *n in names){
if([s caseInsensitiveCompare:n] == NSOrderedSame){
NSLog(@"Match found: %@.\n", n);
}
}
}
}
return 0;
}