Some troubles with challenge 2

int totalcount = 0; // start a counter
          for (NSString *n in names){ // go through propernames dictionary
                for (NSString *o in words){// go through the words dictionary
                    if ([o isEqualToString:[n lowercaseString]]){ // if the lowercase version of the propername is the same as the regular word
                                totalcount++; // increment the counter
                                NSLog(@"%@ AND %@\n", n, o); // print out the matching propername and word 
            NSLog(@"the total number of matches = %i", totalcount); // what was the total count

the trouble i am having is that my total count is 294 and i get an extra AND
somehow the loop is going through one extra time.
the only explanation i can come up with is that the two dictionary files have and extra line at the end that my code is reading as the same and outputting them.
this is my current output:
2014-02-25 19:56:13.153 intNames[1625:303] Win AND win
2014-02-25 19:56:13.451 intNames[1625:303] Wolf AND wolf
2014-02-25 19:56:13.652 intNames[1625:303] Woody AND woody
2014-02-25 19:56:13.806 intNames[1625:303][color=#FF0000] AND [/color]
2014-02-25 19:56:13.806 intNames[1625:303] the total number of matches = [color=#FF0000]294[/color]

thanks everyone

Yes, there is a newline character at the end of both files. Unfortunately, NSString is handling it as a word.

any ideas if i want the program to skip over those?
would something like this work?
like if (the string is a " " then skip it)
and put my original if statement as an else?

i guess my real question is how come other people’s programs didn’t count the " " even though they didn’t account account for it?

ok. i tried to get rid of the extra entry that is just a space by using an if statement before comparing the two strings.

if (![n isEqualToString:@" "])
but i still get the same output.
2014-02-26 19:52:24.358 intNames[4127:303] Woody AND woody
2014-02-26 19:52:24.538 intNames[4127:303] AND
2014-02-26 19:52:24.538 intNames[4127:303] the total number of matches = 294

now im really lost.

Try finding out what kind of word NSString translates a newline character to. Maybe it is translating to the null string?

NSString *const  wordsFile = @"/usr/share/dict/words";
NSString *wordsStr = [NSString stringWithContentsOfFile:wordsFile encoding:NSUTF8StringEncoding error:nil];
NSArray *words = [wordsStr componentsSeparatedByString:@"\n"];
NSLog (@">%@<", [words lastObject]);

:smiley: I had exactly same problem, thanks Ibex10 for the lastObject tip. Here is my code for the solution.

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

int main(int argc, const char * argv[])

@autoreleasepool {
    // Read in at the files and break contents into array
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
    NSArray *words = [wordString componentsSeparatedByString:@"\n"];
    int wordcount = 0; // used as a match counter
    NSString *erroneous = [words lastObject];// to get red of newline character in count which is last object in each array

    // use nested for loops to compare the proper names to the words from the word file. As the word file also contains all the Proper names as well, The proper names are 1st converted to lower case so that we only get matches to words and not the proper names.

// loop construction importanta outer loop has significantly less items in it than inner loop.

    for (NSString *nameToCompare in names) { 
        NSString *lowerCaseName = [nameToCompare lowercaseString];
        for (NSString *wordToCompare in words) {
         // finds matches returning an NSComparison result called match
            NSComparisonResult match = [wordToCompare localizedCompare:lowerCaseName]; 

            if (wordToCompare != erroneous ) { // removes erroneous last array item
                if (match == NSOrderedSame) { // When wordToCompare returns NSOrderedSame means that the 2 items being compared are completly equal
                    NSLog(@"Match Count %i: %@", wordcount, wordToCompare);
                    break; // this makes the program more efficient as once it finds the proper name breaks out of for loop and starts checking for next proper name


    // NS Log provides the summary information below
    NSLog(@" The propernames file contains %lu names", [names count]);
    NSLog(@" The words file contains %lu words", [words count]);
    NSLog(@"Total Proper names that are words found is %i", wordcount);

return 0;


thanks ibex and jimbo.
this was driving me nuts.