首页 > 代码库 > 将字符串依照单词逆序

将字符串依照单词逆序

/*2.将字符串依照单词逆序 
 输入一段字符串,已知字符串仅仅由字母和空格构成。将字符串依照单词逆序
 传入@"welcome to beijing" 返回 @"beijing to welcome"
 */
+ (NSString *)reverseWordsInString:(NSString *)str
//{
//    NSArray * arr= [str componentsSeparatedByString:@" "];
//    NSArray * arr1=[[arr reverseObjectEnumerator]allObjects];
//    NSString * str1 = [arr1 componentsJoinedByString:@" "];
//    return  str1;//[arr copy];
//}

//{
//    NSArray * array = [str componentsSeparatedByString:@" "];
//    NSArray * array1 = [[array reverseObjectEnumerator]allObjects];
//    NSString * str1 = [array1 componentsJoinedByString:@" "];
//    return  str1;
//}

{
    NSArray * array = [str componentsSeparatedByString:@" "];
    NSArray * array1 = [[array reverseObjectEnumerator]allObjects];
    NSString * str1 = [array1 componentsJoinedByString:@" "];
    return  str1;
}

将字符串依照单词逆序