Thank you very much for your answer ibex10! Your code really helped me put to see what was going on and why it wasn’t working so thanks a bunch!
I did made som progress with my code and I got it to work as I wanted to, but unfortunately I get 1602 matches in the end which is way more then the 294 you are supposed to get (according to various forum posts) but I really don’t understand why I get so many matches…
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Read in the two files, the first for the words and the second for the names
NSString *wordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
//Break them into an array of strings
NSArray *words = [wordsString componentsSeparatedByString:@"\n"];
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
//Run through both arrays, maing each word to lowercase and comparing them. If they are the same both of them will be printed out to the console.
unsigned long i = 0;
for (NSString *w in words) {
for (NSString *n in names) {
NSString *temporaryWord = w;
NSString *temporaryName = n;
NSString *lowerCaseWord = [temporaryWord lowercaseString];
NSString *lowerCaseName = [temporaryName lowercaseString];
if ([lowerCaseWord isEqualToString:lowerCaseName]) {
NSLog(@"%@ is a word which is the same as the name %@\n", temporaryWord, temporaryName);
i++;
}
}
}
NSLog(@"The total number of matches are %lu", i);
}
return 0;
}[/code]
Any help would be greatly appreciated!
EDIT: I saw another thread on this forum where someone said that the total number of names or words was 1309. If you add that to 294 you get 1603 which is too close to my result to be a coincidence. I thought that maybe my program compare the words and names one too many times; If the words-list contains both names and words (eg. “Woody” and “woody”) then both of these will be compared to the name “Woody” in the propernames-file and that would explain my output. Hm…
EDIT#2: I solved it! What I wrote in the previous edit was true, so what I did was that I separated the words and names in the words-file into two different arrays. I then ran through the “names and words-words”-array and the original names array and printed out the names and words that matched! I also found some code here on the forum for removing the last out-print, the blanc match. If someone is interested, you can read my code down below. I got 293 matches!
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Read in the two files, the first for the words and the second for the names
NSString *wordsAndNamesString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
//Break them into an array of strings
NSArray *wordsAndNames = [wordsAndNamesString componentsSeparatedByString:@"\n"];
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
//Initzialize two other arrays, one for the word-part of the word-list and one for the name-part of said list
NSMutableArray *wordsAndNamesWords = [[NSMutableArray alloc] init];
NSMutableArray *wordsAndNamesNames = [[NSMutableArray alloc] init];
//Go through the word list one word at the time. If the word is equal to itself with only lowercase letters it's a word and gets placed in the 'wordsAndNamesWords'-Array. If not, then it's a name (capitalized first letter) and gets placed in the 'wordsAndNamesNames'-array.
for (NSString *wAn in wordsAndNames) {
if ([wAn isEqualToString:[wAn lowercaseString]]) {
[wordsAndNamesWords addObject:wAn];
} else {
[wordsAndNamesNames addObject:wAn];
}
}
//Run through both arrays, the original names array directly from the file and the array with the words from the 'propernames'-file, making each word to lowercase and comparing them. If they are the same both of them will be printed out to the console.
//This unsigned long is for keeping track on how many words matches up with how many names.
unsigned long i = 0;
for (NSString *w in wordsAndNamesWords) {
for (NSString *n in names) {
//The line below is for making sure that the last match printed out won't be a blanc.
if (![w isEqualToString:n]) {
NSString *lowerCaseWord = [w lowercaseString];
NSString *lowerCaseName = [n lowercaseString];
if ([lowerCaseWord isEqualToString:lowerCaseName]) {
NSLog(@"%@ is a word which is the same as the name %@\n", w, n);
i++;
}
}
}
}
NSLog(@"The total number of matches are %lu", i);
}
return 0;
}
[/code]