I am new to Xcode and iPhone developing. I am stuck in a problem.
I have two textFields and i want to compare these strings. If there is any common character then it should be omitted/deleted, and the new string is displayed in another textfield.
The common character can be at any location in the string and only one character is omitted at a time(in for loop).
I just wrote this! I havent found cases where it doesnt work! Tell me if it works for you!
NSMutableString *shortString; //put your shortest string in here
NSMutableString *longString; //put your longest string in here
//index for characters to be removed in short string
int characterIndexesShort[shortString.length], characterIndexesLong[longString.length];
int commonCharactersShort = 0, commonCharactersLong = 0;
int cut = 0;
for(int i = 0; i < shortString.length; i++)
{
int oldLongCharCount = commonCharactersLong;
char currentLetter = [shortString characterAtIndex:i];
for(int j = 0; j < longString.length; j++)
{
if(currentLetter == [longString characterAtIndex:j])
characterIndexesLong[commonCharactersLong++] = j;
}
if(commonCharactersLong != oldLongCharCount)
characterIndexesShort[commonCharactersShort++] = i;
}
//At this point you will have arrays containing the indexes of the common characters in both strings
for(int i = 0; i < commonCharactersLong; i++)
{
NSRange range;
range.location = characterIndexesLong[i];
range.length = 1;
[longString replaceCharactersInRange:range withString:@""];
}
for(int i = 0; i < commonCharactersShort; i++)
{
NSRange range;
range.location = characterIndexesShort[i] - cut;
range.length = 1;
[shortString replaceCharactersInRange:range withString:@""];
cut++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.