TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
楼主 |
发表于 2014-1-9 22:29:54
|
显示全部楼层
方法练习:
1、创建一个字符串"ABCDEFG"
2、将字符串中的字符放到一个可变数组中;
3、将可变数组逆序遍历;
4、将逆序遍历的数组追加到一个可变字符串中
程序实现:- //
- // main.m
- // RecodeString
- //
- // Created by yusian on 14-1-9.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
-
- NSString * str = @"ABCDEFG";
- NSLog(@"%@",str);
- NSMutableString * mutableString = [[NSMutableString alloc] init];
-
- NSUInteger length = [str length];
- NSMutableArray * mutableArray = [[NSMutableArray alloc] init];
-
- for (int i = 0; i < length; i++) {
- NSString * temp = [[NSString alloc] initWithFormat:@"%C",[str characterAtIndex:i]];
- [mutableArray addObject:temp];
- [temp release];
- }
-
- NSEnumerator * enumerator = [mutableArray reverseObjectEnumerator];
- id obj;
- while (obj = [enumerator nextObject]) {
- [mutableString appendFormat:@"%@",obj];
- }
-
- NSLog(@"%@",mutableString);
- [mutableString release];
-
-
- }
- return 0;
- }
复制代码 输出结果:- 2014-01-09 22:29:22.894 RecodeString[5674:303] ABCDEFG
- 2014-01-09 22:29:22.896 RecodeString[5674:303] GFEDCBA
- Program ended with exit code: 0
复制代码
|
|