I have used this code to print an English NSString in reverse order:
NSString *str = term;
NSMutableArray *temp=[[NSMutableArray alloc] init];
for(int i=0;i<[str length];i++)
{
[temp addObject:[NSString stringWithFormat:@"%c",[str characterAtIndex:i]]];
}
temp = [NSMutableArray arrayWithArray:[[temp reverseObjectEnumerator] allObjects]];
NSString *reverseString=@"";
for(int i=0;i<[temp count];i++)
{
reverseString=[NSString stringWithFormat:@"%@%@",reverseString,[temp objectAtIndex:i]];
}
NSLog(@"%@",reverseString);
but this code works only with English words, so when I try to use an Arabic word like this: تجريب the console will show random English characters.
how can I use this code to print the reversed version of the above word (and any other similar Arabic (unicode) strings) ?
You need to use
%C(uppercase C) instead of%c(lowercase c) format specifier in the first call tostringWithFormat. The lowercase ‘c’ format specifier is for 8-bit characters; the uppercase ‘C’ is for unicode.EDIT : If you want to do it more efficiently, copy the data into an array of
unicharelements, reverse the array, and feed the result into anotherNSString. This lets you reverse the string inLength/2steps: