My Solution for the second challege


#1

Instead of just finding if Glen and glen are equivalent. I tested all the words and names.

[code]int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//Read in names file as a huge string
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
												 encoding:NSUTF8StringEncoding
													error:NULL];

//Read in words file as a huge string
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
												 encoding:NSUTF8StringEncoding
													error:NULL];

//Break word file string into an array of strings
NSArray *words = [wordString componentsSeparatedByString:@"\n"];


//Break name file string into an array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];

//Go through the arrays
for (NSString *n in names) {
	for (NSString *w in words) {
		if ([w caseInsensitiveCompare:n] == NSOrderedSame) {
			NSLog(@"%@ is equal to %@", n, w);
		}
	}
}
[pool drain];
return 0;

}[/code]

but, I was wondering how could I just pick out that ‘glen’ and ‘Glen’ are equivalent and print that out only?


#2

The way I read it, there were 3 challenges: the one with the groceries list, the one where you compare the words and names, and this last one where you test wether “Glen” and “glen” are equivalent or not.
For this last challenge, this was my solution:

if ([@"Glen" caseInsensitiveCompare:@"glen"] == NSOrderedSame) { NSLog(@"Glen and glen are equivalent"); }
Of course, you could also use NSString variables, put the names in there, and compare these variables. I didn’t see the need for that in this case…