1、 方式一
- (NSString *)reversalString:(NSString *)originString{
NSString *resultStr = @"";
for (NSInteger i = originString.length -1; i >= 0; i--) {
NSString *indexStr = [originString substringWithRange:NSMakeRange(i, 1)];
resultStr = [resultStr stringByAppendingString:indexStr];
}
return resultStr;
}
2、方式二
void char_reverse(char *cha){
char *begin = cha;
char *end = cha + strlen(cha) - 1;
while (begin <= end) {
char temp = *end;
*(end--) = *begin;
*(begin++) = temp;
}
}
// 调用
char cha[] = "hello,world";
char_reverse(cha);
printf("%s",cha);
结果:dlrow,olleh