Challenge 2 finally finished


#1

I had a lot of trouble with this one. I was determined not to peek at other peoples answers. My code is below[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];

    NSArray *wordNameArray = [wordString componentsSeparatedByString:@"\n"];
  
   
    unsigned long wc = [wordNameArray count];
    for (int i = 0; i < wc - 1; i++) 
      {
        NSString *wn = wordNameArray[i];
        NSString *nWn = wordNameArray[i+1];
        if ((wn && [wn caseInsensitiveCompare:nWn] == NSOrderedSame) && ([wn length] > 1) && ([nWn length] > 1)) 
          {
            NSLog(@"%@ %@", nWn, wn); 
          }                      
      }
  }
return 0;

}[/code]output:[quote]abelite Abelite
abigail Abigail
abu Abu
academic Academic
. . .
zeuglodon Zeuglodon
zipper Zipper
zoa Zoa
zwieback Zwieback
[/quote]

I avoided the single letters a, A from being printed because I knew they all couldn’t be names by putting . . . && ([wn length] > 1) && ([nWn length] > 1) in the if statement.

[color=#004040]JR[/color]


#2

Here is my code for challenge 2:

#import <Foundation/Foundation.h>

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

    //Integer to record the number of matches when comparing the two text
    //files later on.
    NSInteger numOfMatches = 0;
    
    // Read in a file as a huge string (ignoring the possibility of an error)
    //reading the propernames file to an NSString Object
    NSString *nameStringProperNames =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    //reading the words file to an NSString object
    NSString *nameStringCommonWords =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    
    
    
    // Break them both into an array of strings
    
    //ProperNames NSString broken down into an array of sub strings, using the new line character as a delimeter
    NSArray *properNames = [nameStringProperNames componentsSeparatedByString:@"\n"];
    
    //Common words NSString broken down into array of sub strings, using the new line character as a delimeter
    NSArray *commonNames = [nameStringCommonWords componentsSeparatedByString:@"\n"];
    
    // Iterate/Go through the proper names array one index at a time and compare it to the common names
    //one index at a time to see if you find a lower case match
    
    //assign each string in the properNames array to the pn variable
    for (NSString *pn in properNames) {
        
        //make it lowercase
        NSString *lowerpn = [pn lowercaseString];
        
        //Assign each string in the commonNames array to the cn variable
        for (NSString *cn in commonNames){
            
            //check to see if the lower case propername is equal to the current common name string
            if ([cn isEqualTo:lowerpn]){
                //if so, print the current common name
                NSLog(@"%@", cn);
                //increment the numOfMatches variable to record the total number of matches
                numOfMatches +=1;
                
            }

        }
        
    }
    //Print the total number of matches
    NSLog(@"The total number of matches is %lu.", numOfMatches);
    
    }
    

return 0;

}