title: Correct answer - comparing strings
body: <p>I'm building my own alternative to the Levenshtein distance to train myself in objective-c. This class will get a few sentences, only 2 during initial testing and return how many percent correct the sentence is. How many percent matches with the "correct sentence".</p>
  
  <p>Basically what it does is it gets 2 sentences, (only 2 words during initial testing really) and calculates. But I get some strange NSLog's.</p>
  
  <p>If I enter 1989 and 199 it'll return 75% correct - which is the correct answer. However, if I enter "orange" and "oange" - it returns 50% correct when correct is 83% (right??)</p>
  
  <p>This is the code calling the method:</p>
  
  <pre><code>    -(void)compare {
  // Take each and every sentence from the users source and check it against the other sources. If it contains 2 or more words/numbers that are equal, i'll get "investigated" further
  
  NSMutableArray *userSentences = [[MyManager sharedManager] contentSentencesList];
  NSMutableArray *serverSentences = [NSMutableArray arrayWithArray:getSentencesFromText(serverText)];
  
  // Sample sentences
  [userSentences insertObject:@"oange" atIndex:userSentences.count];
  [serverSentences insertObject:@"orange" atIndex:serverSentences.count];
  
  // For-statement integers
  int i = 0;
  int b = 0;
  
  for (i = 0; i &lt; userSentences.count; i++) {
      // Check first sentence
  
      NSString *userSentence;
      NSString *serverSentence;
  
      // Check similarity of the two sentences, get percent and add to centerPercent
      for (b = 0; b &lt; serverSentences.count; b++) {
          // Compare sentences
          userSentence = userSentences[i];
          serverSentence = serverSentences[b];
          // Compare sentences with custom class
  
          // Initialize Distance class
          SourcerDistance *wordDistance = [[SourcerDistance alloc] init];
  
          // Create resultPercent integer and calculate it
          int resultPercent = [wordDistance distanceBetween:userSentence serverSentence:serverSentence];
  
          NSLog(@"%@%d", @"FinishViewController result: ", resultPercent);
  
          // Add resultPercent to averagePrecent and increase averagPercentToDivide by 1
          centerPercent = centerPercent + resultPercent;
          centerPercentToDivide++;
      }
  
  
      // Set and display resultoppositeSpelling
      // averagePercent = centerPercent / centerPercentToDivide;
  
      // Use an integer to remove decimals
      [self presentResult];
  }
  }
  </code></pre>
  
  <p>and this is the other class:</p>
  
  <pre><code>#import "SourcerDistance.h"
  
  @implementation SourcerDistance
  
  -(int)distanceBetween:(NSString *)userSentence serverSentence:(NSString *)serverSentence {
  
  // Declare the 2 arrays containing all the words from the user's source and the developer's source
  NSArray *developerSourceSentence = [self getWords:serverSentence];
  NSArray *userSourceSentence = [self getWords:userSentence];
  
  // Declare variables that'll be use for for-statements
  int developerWordsLoop = developerSourceSentence.count;
  int userWordsLoop = userSourceSentence.count;
  
  // Declare variables required for matching (average of all words)
  float centerPercent = 0; // This is for ALL words in total
  float centerPercentToDivide = 0; // This is for all words in total
  
  // Single-word variables
  float totalCharacters = 0;
  float matchingCharacters = 0;
  float percentMatchingSingleWord = 0;
  
  NSLog(@"%@%d", @"userSourceSentenceCount: ", userSourceSentence.count);
  NSLog(@"%@%d", @"developerSourceSentenceCount: ", developerSourceSentence.count);
  
  // Loop through all of the user words
  for (userWordsLoop = 0; userWordsLoop &lt; userSourceSentence.count; userWordsLoop++) {
  
      // Loop through all of the developer words
      for (developerWordsLoop = 0; developerWordsLoop &lt; developerSourceSentence.count; developerWordsLoop++) {
  
          // Declare variables that contain all the characters of the user- and developer-words
          NSMutableArray *userCharacters = [self getCharacters:userSourceSentence[userWordsLoop]];
          NSMutableArray *developerCharacters = [self getCharacters:developerSourceSentence[developerWordsLoop]];
  
          // Compare characters
          matchingCharacters = [self compareCharacters:userCharacters developerCharacters:developerCharacters];
  
  
          // Set the variables
          totalCharacters = developerCharacters.count;
          percentMatchingSingleWord = matchingCharacters / totalCharacters * 100;
  
          NSLog(@"%@%f", @"totalCharacters", totalCharacters);
          NSLog(@"%@%f", @"matchingCharacters", matchingCharacters);
          NSLog(@"%@%f", @"iterate", percentMatchingSingleWord);
          centerPercent = centerPercent + percentMatchingSingleWord;
          centerPercentToDivide++;
      }
  }
  
  // Declare variables used with final result
  float finalPercentFloat = 0;
  int finalPercent = 0;
  NSLog(@"%@%f", @"centerPercent: ", centerPercent);
  NSLog(@"%@%f", @"centerPercentToDivide: ", centerPercentToDivide);
  finalPercentFloat = centerPercent/centerPercentToDivide;
  NSLog(@"%@%f", @"finalPercent: ", finalPercentFloat);
  finalPercent = (int)finalPercentFloat;
  return finalPercent;
  
  }
  
  -(float)compareCharacters:(NSMutableArray *)userCharacters developerCharacters:(NSMutableArray *)developerCharacters {
  // Declare result variables and other required variables
  float matchingCharacters;
  int userCharactersLoop = 0;
  int developerCharactersLoop = 0;
  
  // Loop through all of the userCharacters
  for (userCharactersLoop = 0; userCharactersLoop &lt; userCharacters.count; userCharactersLoop++) {
      // Loop through all of the developerCharacters
      for (developerCharactersLoop = 0; developerCharactersLoop &lt; developerCharacters.count; developerCharactersLoop++) {
          // Match every character here
          if ([userCharacters[userCharactersLoop] isEqualToString:developerCharacters[developerCharactersLoop]]) {
              // Increase matchingCharacters
              matchingCharacters++;
          } else {
              // Do nothing
          }
      }
  }
  
  // Return result variable
  return matchingCharacters;
  }
  
  -(NSArray *)getWords:(NSString *)sentence {
  // Get words of sentence from developer-source
  NSArray *sentenceWords;
  NSString *serverSentenceToRead = sentence;
  sentenceWords = [serverSentenceToRead componentsSeparatedByCharactersInSet:
                             [NSCharacterSet characterSetWithCharactersInString:@":;.!? "]
                             ];
  
  // Return developer words
  return  sentenceWords;
  }
  
  -(NSMutableArray *)getCharacters:(NSString *)word {
  NSMutableArray *wordCharacters = [[NSMutableArray alloc] initWithCapacity:[word length]];
  for (int i=0; i &lt; [word length]; i++) {
      NSString *ichar  = [NSString stringWithFormat:@"%c", [word characterAtIndex:i]];
      [wordCharacters addObject:ichar];
  }
  // Return the characters of the word
  return wordCharacters;
  }
  
  @end
  </code></pre>
  
  <p>NSLog:</p>
  
  <pre><code>2014-09-03 20:22:32.015 Sourcer[27532:60b] userSourceSentenceCount: 1
  2014-09-03 20:22:32.017 Sourcer[27532:60b] developerSourceSentenceCount: 1
  2014-09-03 20:22:32.018 Sourcer[27532:60b] totalCharacters6.000000
  2014-09-03 20:22:32.018 Sourcer[27532:60b] matchingCharacters3.001519
  2014-09-03 20:22:32.019 Sourcer[27532:60b] iterate50.025322
  2014-09-03 20:22:32.020 Sourcer[27532:60b] centerPercent: 50.025322
  2014-09-03 20:22:32.021 Sourcer[27532:60b] centerPercentToDivide: 1.000000
  2014-09-03 20:22:32.021 Sourcer[27532:60b] finalPercent: 50.025322
  2014-09-03 20:22:32.022 Sourcer[27532:60b] FinishViewController result: 50
  2014-09-03 20:22:32.022 Sourcer[27532:60b] averagePercent (float): 50.000000
  </code></pre>
  
  <p><strong>What am I doing wrong here? Is it possible for anyone to understand the code and help me find out what's wrong? There's something odd about this algorithm</strong></p>
  
  <p>Thanks a lot!</p>
  
  <p>(I know I'm kinda reinventing the wheel, but I want to try :))</p>
  
  <p>Sincerely,
  Erik</p>