Solution to challenge "Interesting names"

So i’ve been struggling with this for quite a while now, but I simply can’t figure out what is going wrong with my code. It prints out several words but not “wolf”. Thanks in advance!

[code] NSString *nameString =
[NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];

    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    
    //Save last item
    NSMutableString *lastOne = [[NSMutableString alloc]init];
    
    // Go through the array one string at a time
    for (NSString *n in names) {
        
       //compare to last one
        if ([n caseInsensitiveCompare:lastOne] == NSOrderedSame) {
            NSLog(@"%@", lastOne);
        }
        
        [lastOne setString:[n lowercaseString]];
        
        }[/code]

Here is an extract from the output produced by the code fragment you have provided:

...
2014-03-20 13:10:37.397 PrintWolf[41635:303] windbreaker
2014-03-20 13:10:37.397 PrintWolf[41635:303] wistaria
2014-03-20 13:10:37.397 PrintWolf[41635:303] wisteria
2014-03-20 13:10:37.398 PrintWolf[41635:303] wolf
2014-03-20 13:10:37.398 PrintWolf[41635:303] woodruff
2014-03-20 13:10:37.399 PrintWolf[41635:303] woody
2014-03-20 13:10:37.399 PrintWolf[41635:303] wren
...

It does contain the word wolf. What exactly are you trying to do?

Please provide us with your whole code ellipsisfeathomer. The part you gave looks like you’re only wanting to lowercase your last word ! So what’s the point for the challenge ?

I would also like to provide you with my first code I made. The idea was great but did not give the result wanted in this challenge.

void findMatchingNamesAndWords(NSArray *iterateFrom, NSArray *whereTo)
{

    // Go through the array one string at a time
    for (NSString *what in iterateFrom)
    {
        // Go through the other array one string at a time
        for (NSString *match in whereTo)
        {
        // Case Insensitive check for a matching string
        NSComparisonResult leResultat = [what caseInsensitiveCompare:match];

        // If found write it to the console and no need to go further...
        if (leResultat == NSOrderedSame)
        {
            NSLog(@"%@ is the same as %@", what, match);
            break;
        }
        }

    }

}

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

        // Read in a file as a huge string (ignoring the possibility of an error)
        NSString *nameString =
        [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                  encoding:NSUTF8StringEncoding
                                     error:NULL];
        NSString *wordString =
        [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                  encoding:NSUTF8StringEncoding
                                     error:NULL];

        // Break it into an array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];

        // Choose the shortest NSArray to iterate from
        NSUInteger sizeOfNames = [names count];
        NSUInteger sizeOfWords = [words count];

        if (sizeOfNames < sizeOfWords)
        {
            findMatchingNamesAndWords(names, words);
        } else {
            findMatchingNamesAndWords(words, names);
        }

    }
    return 0;
}

Can you figure it out why ?
Consider it as another Challenge :wink:
I did succeed in finding why and how to make it work tho :slight_smile:

[code] NSString *nameString =
[NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];

    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    
    //Save last item
    NSMutableString *lastOne = [[NSMutableString alloc]init];
    
    // Go through the array one string at a time
    for (NSString *arraystring in names) {
        
        //compare lastOne and arraystring
        if ([arraystring caseInsensitiveCompare:lastOne] == NSOrderedSame) {
            NSLog(@"%@", lastOne);
        }
        
        [lastOne setString:arraystring]
        
        }[/code]

The concept behind it is as follows: the loop goes through every item of the array, saving the last one it went through to an outside variable. the next time, it checks whether the current item is equal to the previous one (via the previously saved variable). if they are (which is supposed to be the case only with “wolf” and “Wolf”), then it should print out the item but unfortunately it doesn’t.
PS: the code I provided earlier was a little messy, caseInSensitiveCompare and setting the variable to lower case is pointless :wink:

Problem solved, but now I feel really stupid. :smiley: The code does just what it’s supposed to do. I checked out the “words” file and noticed that in fact everything printed out was in there twice in a row.

Could you guys please check if the code is good. It gives to many matches, but it seems to be correct. I’m absolutely new in Objective-C, so I would like to know your opinions:

#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 *nameString =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    // Break it into an array of strings
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    
    NSString *wordString =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    // Go through the array one string at a time
    for (NSString *n in names) {
        // Look for the string "aa" in a case-insensitive manner
        NSRange r = [wordString rangeOfString:n options:NSCaseInsensitiveSearch];
        
        // Was it found?
        if (r.location != NSNotFound) {
            unichar *buf = malloc(r.length*sizeof(unichar));
            [wordString getCharacters:buf range:r];
            NSString *result = [NSString stringWithCharacters:buf length:r.length];
            printf("\"%s\" in the names list matches \"%s\" in the words list\n", [n UTF8String], [result UTF8String]);
        }
    }
    
}
return 0;

}