iPhone SDK - Comparing characters in string

Posted by Karl Daniel on Stack Overflow See other posts from Stack Overflow or by Karl Daniel
Published on 2012-11-13T12:39:55Z Indexed on 2012/11/15 11:02 UTC
Read the original article Hit count: 412

Filed under:
|
|
|

Basically what I'm trying to do is compare 2 strings one from a plist and one from the user's input. I use a while loop to step through each character and compare it and if true then I increase an integer then once the loop has finished I work out the percentage correct / similarity of the plist answer and the user's answer. I seem to be having a problem however as the only return I'm getting is 0. Below is the code I'm using...

The code below is all functioning and the question no longer requires answering...

Working code...

    answerLength = boxAnswer.length; //Gets number of characters of first string.
    plistLength = plistAnswer.length; //Gets number of characters of second string.

    characterRange = 0; //Sets the variable for which character to look at.
    charactersCorrect = 0; //Sets the variable of number of matching characters.

    unichar answerCharacter; //Declares a unichar for the first string.
    unichar plistCharacter; //Declares a unichar for the second string.

    while (answerLength > 0 && plistLength > 0) {

        answerCharacter = [boxAnswer characterAtIndex:characterRange]; //Gets character of first string at the index of the range integer.
        plistCharacter = [plistAnswer characterAtIndex:characterRange]; //Gets character of second string at the index of the range integer.

        answerLength--; //Reduces number of characters left to compare.
        plistLength--; 
        characterRange++; //Increases integer to tell it to look at next character in string.

        if (answerCharacter == plistCharacter) { //Checks to see if character of first string and character of second string match.
                charactersCorrect++; //If true increases the number correct.
        }

    }

    //Works out percentage of matching characters out of the total string.

    totalChar = plistAnswer.length;
    totalPercentage = (charactersCorrect/totalChar)*100;
    percentageCorrect.text = [NSString stringWithFormat:@"%i%%",totalPercentage];

Variable Declarations...

int answerLength;

int plistLength;

int characterRange;

double totalChar;
double charactersCorrect;

int totalPercentage;

© Stack Overflow or respective owner

Related posts about iphone

Related posts about string