Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 649317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:55:52+00:00 2026-05-13T21:55:52+00:00

How can I calculate the number of differences between two NSStrings. Example: NSString 1

  • 0

How can I calculate the number of differences between two NSStrings.

Example:

NSString 1 = "this is a string"

NSString 2 = "Tihs isa string"

should return: 4 (one for the capital “T”, one for the “i”, the “h” and for the missing space)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:55:52+00:00Added an answer on May 13, 2026 at 9:55 pm

    What you’re looking for is the Levenshtein Distance.

    An implementation in Objective-C:

    ------------------------------------------------------------------------ 
    
    //
    //  NSString-Levenshtein.h
    //
    //  Created by Rick Bourner on Sat Aug 09 2003.
    //  rick@bourner.com
    
    @interface NSString(Levenshtein)
    
    // calculate the smallest distance between all words in stringA and stringB
    - (float) compareWithString: (NSString *) stringB;
    
    // calculate the distance between two string treating them each as a
    // single word
    - (float) compareWithWord: (NSString *) stringB;
    
    // return the minimum of a, b and c
    - (int) smallestOf: (int) a andOf: (int) b andOf: (int) c;
    
    @end
    
    --------------------------------------------------------------------
    
    //
    //  NSString-Levenshtein.m
    //
    //  Created by Rick Bourner on Sat Aug 09 2003.
    //  Rick@Bourner.com
    
    #import "NSString-Levenshtein.h"
    
    
    @implementation NSString(Levenshtein)
    
    // calculate the mean distance between all words in stringA and stringB
    - (float) compareWithString: (NSString *) stringB
    {
         float averageSmallestDistance = 0.0;
         float smallestDistance;
         float distance;
    
         NSMutableString * mStringA = [[NSMutableString alloc]  initWithString: self];
         NSMutableString * mStringB = [[NSMutableString alloc]  initWithString: stringB];
    
    
         // normalize
         [mStringA replaceOccurrencesOfString:@"\n"
                                  withString: @" "
                                     options: NSLiteralSearch
                                       range: NSMakeRange(0, [mStringA  length])];
    
         [mStringB replaceOccurrencesOfString:@"\n"
                                  withString: @" "
                                     options: NSLiteralSearch
                                       range: NSMakeRange(0, [mStringB  length])];
    
         NSArray * arrayA = [mStringA componentsSeparatedByString: @" "];
         NSArray * arrayB = [mStringB componentsSeparatedByString: @" "];
    
         NSEnumerator * emuA = [arrayA objectEnumerator];
         NSEnumerator * emuB;
    
         NSString * tokenA = NULL;
         NSString * tokenB = NULL;
    
         // O(n*m) but is there another way ?!?
         while ( tokenA = [emuA nextObject] ) {
    
             emuB = [arrayB objectEnumerator];
             smallestDistance = 99999999.0;
    
             while ( tokenB = [emuB nextObject] )
                 if ( (distance = [tokenA compareWithWord: tokenB] ) <  smallestDistance )
                     smallestDistance = distance;
    
             averageSmallestDistance += smallestDistance;
    
         }
    
         [mStringA release];
         [mStringB release];
    
         return averageSmallestDistance / [arrayA count];
    }
    
    
    // calculate the distance between two string treating them eash as a
    // single word
    - (float) compareWithWord: (NSString *) stringB
    {
         // normalize strings
         NSString * stringA = [NSString stringWithString: self];
         [stringA stringByTrimmingCharactersInSet:
                   [NSCharacterSet whitespaceAndNewlineCharacterSet]];
         [stringB stringByTrimmingCharactersInSet:
                   [NSCharacterSet whitespaceAndNewlineCharacterSet]];
         stringA = [stringA lowercaseString];
         stringB = [stringB lowercaseString];
    
    
         // Step 1
         int k, i, j, cost, * d, distance;
    
         int n = [stringA length];
         int m = [stringB length];  
    
         if( n++ != 0 && m++ != 0 ) {
    
             d = malloc( sizeof(int) * m * n );
    
             // Step 2
             for( k = 0; k < n; k++)
                 d[k] = k;
    
             for( k = 0; k < m; k++)
                 d[ k * n ] = k;
    
             // Step 3 and 4
             for( i = 1; i < n; i++ )
                 for( j = 1; j < m; j++ ) {
    
                     // Step 5
                     if( [stringA characterAtIndex: i-1] == 
                          [stringB characterAtIndex: j-1] )
                         cost = 0;
                     else
                         cost = 1;
    
                     // Step 6
                     d[ j * n + i ] = [self smallestOf: d [ (j - 1) * n + i ] + 1
                                                 andOf: d[ j * n + i - 1 ] +  1
                                                 andOf: d[ (j - 1) * n + i -1 ] + cost ];
                 }
    
             distance = d[ n * m - 1 ];
    
             free( d );
    
             return distance;
         }
         return 0.0;
    }
    
    
    // return the minimum of a, b and c
    - (int) smallestOf: (int) a andOf: (int) b andOf: (int) c
    {
         int min = a;
         if ( b < min )
             min = b;
    
         if( c < min )
             min = c;
    
         return min;
    }
    
    @end
    

    Author of the source above: Rick Bourner, http://www.merriampark.com/ldobjc.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 394k
  • Answers 394k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What the query should do and calculate with the values… May 15, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer When you instantiate a Date object, you can pass in… May 15, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer It returns an ICollectionView that is not a ListCollectionView. You… May 15, 2026 at 2:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.