Alternate solution/logic correct? Please confirm


#1

I had a hard time figuring this problem out. I read the other posts saying there is a minor problem with the files in the challenge question, so I created my own smaller files to read in to solve this problem. Can someone confirm if my logic/code is correct solution for this problem?
It seems to return the right values, but I’m not sure if it is correct. Thanks everyone.

I created a propernames.rtf file with the following:
Ani
Abigail
Adam
Bruce
Bryan
Carl
Glen
Jack
Marissa
May
Rose
Sherry
Victor
Zach

I created a words.rtf file with the following:
ascend
abigail
cpu
evolution
glen
internet
jack
lan
may
oreo
rose
sherry
wifi
victor
zebra

Then the I ran the following code and it did displayed the matching words, but I’m not sure if my logic in the code is correct.

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

@autoreleasepool {
    
    //read in my own files as a smaller string so program won't take so long to run
   NSString *propernamefile = [NSString stringWithContentsOfFile:@"/Users/smith/Documents/propernames.rtf" encoding:NSUTF8StringEncoding error:NULL];
   NSString *wordfile = [NSString stringWithContentsOfFile:@"/Users/smith/Documents/words.rtf" encoding:NSUTF8StringEncoding error:NULL];
   
    //Break it into an array of strings
    NSArray *ProperNames= [propernamefile componentsSeparatedByString:@"\n"];
    NSArray *words= [wordfile componentsSeparatedByString:@"\n"];
    
    //Go through the array one string at a time
    //Look for the any string that is a common proper name and also a regular word.
    
    for (NSString *propername in ProperNames) {
        for (NSString *word in words){
            
            //what is the lowercase version of this propername and set it to equal to looklow
            NSString *looklow = [propername lowercaseString];
            //what is the capital version of this regular word and set it to equal to lookcap
            NSString *lookcap = [word capitalizedString];
        
            
           //Solution 1 using caseInsensitiveCompare
           //If the lowercase of the propername is equivalent to the capitalization of the regular word according to the caseInsensitiveCompare, then the word is the same. 
           if ([looklow caseInsensitiveCompare: lookcap] == NSOrderedSame) {
                 NSLog(@"%@, %@", looklow, lookcap);
           }
        
         
            //Alternate Solution 2 using NSRange
            //NSRange r = [propername rangeOfString:lookcap options:NSCaseInsensitiveSearch];
            //NSRange s = [word rangeOfString:looklow options:NSCaseInsensitiveSearch];
            
            //if ( (r.location != NSNotFound) && (s.location != NSNotFound) ){
            //    NSLog(@"%@, %@", propername, word);
            //}
            
            
        }
    }

    
}
return 0;

}


#2

[quote]
//what is the lowercase version of this propername and set it to equal to looklow
NSString *looklow = [propername lowercaseString];
//what is the capital version of this regular word and set it to equal to lookcap
NSString *lookcap = [word capitalizedString];
[/quote]
Capitalizing the word is not really necessary because you are using the caseInsensitiveCompare later in the if statement.

Also, next time, please use the Code tags to post your code or any output from it; this will make it easier to view the content.


#3

Thanks for your reply. I fixed it to below in bold and it seems to work. Is this correct now?

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        //read in my own files as a smaller string so program won't take so long to run
       NSString *propernamefile = [NSString stringWithContentsOfFile:@"/Users/vnvdm/Documents/propernames.rtf" encoding:NSUTF8StringEncoding error:NULL];
       NSString *wordfile = [NSString stringWithContentsOfFile:@"/Users/vnvdm/Documents/words.rtf" encoding:NSUTF8StringEncoding error:NULL];
       
        //Break it into an array of strings
        NSArray *ProperNames= [propernamefile componentsSeparatedByString:@"\n"];
        NSArray *words= [wordfile componentsSeparatedByString:@"\n"];
        
        //Go through the array one string at a time
        //Look for the any string that is a common proper name and also a regular word.
        
        for (NSString *propername in ProperNames) {
            for (NSString *word in words){
                
     
              [b] if ([propername caseInsensitiveCompare: word] == NSOrderedSame) {
                     NSLog(@"%@, %@", propername, word);
               }[/b]
     
            }
        }

        
    }
    return 0;
}