String anyway work sometimes encounter, in fact, with the previous integer reverse output is a truth, of course, there are a lot of methods of implementation, some people write seven kinds of, can achieve good, commonly used to achieve two kinds, make a look at it:
Reverse.h
@interface reverse:nsobject-(void) reverse;-(NSString *) Reverse1: (NSString *) str; @end
REVERSE.M's Code:
@implementation reverse-(void) reverse{ char arr[]= "flyelephant"; Nsinteger Count=strlen (arr); for (Nsinteger i=0; i<count/2; i++) { char temp=arr[i]; ARR[I]=ARR[COUNT-I-1]; arr[count-i-1]=temp; } NSLog (@ "Current string:%s", arr);} -(NSString *) Reverse1: (NSString *) str{ nsstring *[email protected] ""; for (Nsinteger i=str.length-1; i>=0; i--) { char temp=[str characteratindex:i]; Result=[result stringbyappendingstring:[nsstring stringwithformat:@ "%c", temp]]; } return result;} @end
Call:
Reverse *reverse=[[reverse Alloc]init]; [Reverse reverse]; NSLog (@ "The result of the final output is:%@", [Reverse reverse1:@ "Http://www.cnblogs.com/xiaofeixiang"]); NSLog (@ "iOS technology Group: 228407086");
Effect:
Algorithm-string inversion