SomethingIsCrazy
SomethingIsCrazy

Reputation: 23

NSString comparing

I am searching for an algorithm where I can compare two strings and read out the failers.

I know the normal code to compare two strings but this isn't enough.

Example:

NSString *userinput = @"xaplseiPhonr";// (Input from the user)
NSString *correct =@"apple iphone";

In the next step i would check this strings and put out the failers for this example: x,l,s, ,r = 5 failers

I tested many things, saved the two strings in an array with two for-loops and compared it but something is wrong, the hardest thing is if the word has 2 letters that are the same. Or if you have no dynamic index search then are the whole letters after the "l" or the missing space in the userinput false, and so on.

So, It would be nice if anyone has fine code for me


edit: code to compare, but this doesn't work if you missed a letter or space.

for (int i = 0; i < [originalWordLetters count]; i++) 
{
    NSString *originalLetter = [originalWordLetters objectAtIndex:i];

    //NSLog(@"%i: %@", d, originalLetter);
    BOOL letterFound = FALSE;

    while (letterFound == FALSE && d < [userWordLetters count]) 
    {
        if ([originalLetter caseInsensitiveCompare:[userWordLetters objectAtIndex:d]] == NSOrderedSame) 
        {
            //NSLog(@"letter %i correct", d+1);
            letterFound = TRUE;
        }
        else
        {
            //NSLog(@"letter %i false", d+1);
            failedLetters++;
        }
        d++;
    }
}

I have a working algorithm where you can compare two strings. And put out the mistakes

I've uploaded the sample project to GitHub: NSString compareTwoStrings: algorithm outputs the mistakes

Upvotes: 0

Views: 372

Answers (2)

Nicolas Bachschmidt
Nicolas Bachschmidt

Reputation: 6505

What you want to do is compute the Damerau-Levenshtein distance between two strings. Here is an open source implementation for NSString: GitHub JanX2/NSString-DamerauLevenshtein

Upvotes: 1

samir
samir

Reputation: 4551

You can use NSScanner to do this.

Upvotes: 0

Related Questions