Moves in squared strings (I)

2017/12/23 posted in  codeWars


题目链接:https://www.codewars.com/kata/moves-in-squared-strings-i/train/objc

#import <Foundation/Foundation.h>

NSString *vertMirror(NSString *strng) {
    NSMutableString *res = [[NSMutableString alloc] init];
    NSArray *stringArray = [strng componentsSeparatedByString:@"\n"];
    for (NSString *tmpStr in stringArray) {
        NSMutableString * reverseString = [NSMutableString string];
        for(int i = 0 ; i < tmpStr.length; i ++){
            //倒序读取字符并且存到可变数组数组中
            unichar c = [tmpStr characterAtIndex:tmpStr.length- i -1];
            [reverseString appendFormat:@"%c",c];
        }
        
        if ([tmpStr isEqualToString:[stringArray lastObject]]) {
            [res appendString:reverseString];
            break;
        }
        
        [reverseString appendString:@"\n"];
        [res appendString:reverseString];
    }
    
    return res;
}

NSString *horMirror(NSString *strng) {
    NSMutableString *res = [[NSMutableString alloc] init];;
    NSArray *stringAry = [strng componentsSeparatedByString:@"\n"];
    for (int i = stringAry.count-1; i>=0; i--) {
        [res appendString:stringAry[i]];
        if (i==0) {
            break;
        }
        [res appendString:@"\n"];
    }
    return res;
}

NSString *oper(NSString *(*f)(NSString *), NSString *s) {
    return f(s);
}