0

i have created a quiz application for iphone Where i added multiple selection option. Suppose correct ans is ABC and selected ans is AC. Both are string array. how can i compare if the ans is correct or wrong.

3 Answers 3

1

try this code:

    NSString* rgtAns = @"ABC";//right answer
    NSString* ans = @"Ac";//current answer

    rgtAns = [rgtAns uppercaseString];//uppercase the string
    ans = [ans uppercaseString];//if the answer letters are uppercase, you do not need this

    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString: rgtAns] invertedSet];
    NSString *filtered = [[ans componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    BOOL isRight = [ans isEqualToString:filtered];
    NSLog(@"%d",isRight);
Sign up to request clarification or add additional context in comments.

Comments

0

in iOS you should use NSString (or its mutable version, NSMutableString) to work with strings, NSString has a method named isEqualToString: That helps you compare two string objects.

Comments

0
   if ([originalAnswer isEqualToString:userAnswerToCompare]){
   //do some processing here
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.