字符串反转

//
//  main.m
//  字符串逆序
//
//  Created by syrcfwzx on 16/1/9.
//  Copyright (c) 2016年 syrcfwzx. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        2.将字符串按照单词逆序 (10分)
//        
//        输入一段字符串,已知字符串只由字母和空格构成,将字符串按照单词逆序
//        
//        传入@"welcome to qianfeng"
//        
//        返回 @"qianfeng to welcome"
        NSString* str = @"welcome to qianfeng";
//        NSMutableString* reverse = [NSMutableString string];
//        NSInteger len = [str length];
//        for(NSInteger i =len-1 ;i>0;i--)
//        {
//            unichar c =[str characterAtIndex:i];
//            NSString* temp = [NSString stringWithCharacters:&c length:1];
//            [reverse appendString:temp];
//            
//        }
//        NSLog(@"%@",reverse);
//
        NSArray* array = [str componentsSeparatedByString:@" "];
        NSMutableArray* arr = [NSMutableArray array];
        
        for(NSInteger i=[array count]-1;i>=0;i--)
        {
            [arr addObject:array[i]];
        }
            NSString* newstr = [arr componentsJoinedByString:@" "];
        NSLog(@"%@",newstr);
        
    }
    return 0;
}

 

posted @ 2016-01-09 11:22  33sve  阅读(165)  评论(0编辑  收藏  举报