[code] //Declare globals
NSMutableArray *lowerCaseWords = [[NSMutableArray alloc]init];
NSMutableArray *capWords = [[NSMutableArray alloc]init];
//Get file into array
NSString *wordsFile = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *wordsArray = [wordsFile componentsSeparatedByString:@"\n"];
//Seperate words starting with capital letters & lower case into 2 arrays
for (NSString *n in wordsArray){
NSCharacterSet *capsList = [NSCharacterSet uppercaseLetterCharacterSet];
NSRange capMatches = [n rangeOfCharacterFromSet];
if (capMatches.location !=NSNotFound) {
[capWords addObject:n];
}else{
[lowerCaseWords addObject:n];
}
}
//Quick sanity check to make sure the arrays have something in them
NSUInteger capCount = [capWords count];
NSUInteger lowerCount = [lowerCaseWords count];
NSLog(@"Total words in Caps array %lu",capCount);
NSLog(@"Total words in LowerCase array %lu",lowerCount);
//compare the 2 arrays and see if there are any matches displaying them as we go
int y=0;
for (NSString *lC in lowerCaseWords){
for (int x=0;x<capCount;x++){
NSString *pN = capWords[x];
if ([pN caseInsensitiveCompare:lC]==NSOrderedSame){
NSLog(@"%d %@, %@",x,lC,pN);
y=y+1;
}
}
}
//Report total matches
NSLog(@"Total Matches %d",y);
}
return 0;
}
[/code]
Here’s my solution.I chose to use just the words file and split the result into 2 arrays one with words starting with caps and one with the rest, then compare the 2 for matches. It’s probably not the most effecient way but it seems to work.
Anyone know what the correct number of matches is? I got 1515.
Chris