Problem with Challenge 2 - Program won't run

Hello everyone!

I think I have found a solution to challenge 2, but for some reason, when i click run, my program will keep on going and going in the debug menu but never print anything out to the console. I don’t get any warnings or such from the compiler but despite it saying “Building Succeeded” nothing happens in the console but the memory and processor bars in the debug menu will keep on going until I click stop. If anyone could have a look at my code and help me figure out what is wrong, it would be greatly appreciated!

[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.
	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 == lowerCaseName) {
				NSLog(@"%@ is a word which is the same as the name %@\n", temporaryWord, temporaryName);
			}
		}
	}
	
}
return 0;

}[/code]

Thanks in advance!

Good Day iPhone4,

[quote] if (lowerCaseWord == lowerCaseName) { NSLog(@"%@ is a word which is the same as the name %@\n", temporaryWord, temporaryName); } [/quote]
First ask yourself: what is the meaning of the expression lowerCaseWord == lowerCaseName in the above if-statement?

What you wanted to do with that if statement was to compare the values of two string objects for equality.

But that if statement does not do that, simply because you are checking to see if two distinct address values are equal!. That explains why you are not seeing any output.

The reason your program is taking so long to finish is another matter.

You can try the following verbose-output code to see why.

[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.
   
    const unsigned long iterations = [words count] * [names count];
    NSLog (@"Takes %lu iterations...", iterations);

    unsigned long i = 0;
    const unsigned long N = 1000000;
    
    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 == lowerCaseName) {
                NSLog(@"%@ is a word which is the same as the name %@\n", temporaryWord, temporaryName);
            }
            i += 1;
            if (i % N == 0) {
                NSLog (@"%ld of %ld iterations...", i, iterations);
            }
        }
    }
    NSLog (@"%ld of %ld iterations...", i, iterations);
}
return 0;

}[/code]
Output should be something like this:

2015-04-27 12:41:17.345 Killer[2534:230433] Takes 308776083 iterations...
2015-04-27 12:41:18.222 Killer[2534:230433] 1000000 of 308776083 iterations...
2015-04-27 12:41:19.096 Killer[2534:230433] 2000000 of 308776083 iterations...
2015-04-27 12:41:19.955 Killer[2534:230433] 3000000 of 308776083 iterations...
...
2015-04-27 12:45:48.083 Killer[2534:230433] 306000000 of 308776083 iterations...
2015-04-27 12:45:48.954 Killer[2534:230433] 307000000 of 308776083 iterations...
2015-04-27 12:45:49.819 Killer[2534:230433] 308000000 of 308776083 iterations...
2015-04-27 12:45:50.493 Killer[2534:230433] 308776083 of 308776083 iterations...

I hope this helps.

[Become a competent programmer: pretty-function.org]

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! :smiley:

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! :smiley: 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]