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
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);