oc 学习习题答案

/*
 1.对称的数组(10分)
 
 传入一个数组,其元素类型与个数皆未知,返回新数组,由原数组的元素正序反序拼接而成。
 
 //参数arr 表示原数组地址
 
 //返回值是新数组的地址
 
 传入:@[@"one", @"Two", @"Three"]
 
 返回:@[@"one", @"Two", @"Three", @"Three", @"Two", @"one"]
 */
-(NSArray *)symmetryArray:(NSArray *)arr {
    
    NSMutableArray *ary = [NSMutableArray arrayWithArray:arr];
    
    for (int i = 0; i < arr.count ; i++) {
        [ary addObject:[NSString stringWithString:arr[arr.count-1-i]]];
    }

    return (NSArray *)ary;
}

/*
 2.找出数组中最长的字符串(10分)
 
 数组中的元素都是字符串,返回其中最长字符串的地址
 */
-(NSString *)longestStringInArray:(NSArray *)arr {

    NSMutableString *maxString = [NSMutableString stringWithString:arr[0]] ;
    
    for (id obj in arr) {
        
        if ([obj length] > [maxString length]) {
            
            [maxString setString:[NSString stringWithString:obj]];
            
        }
    }

    return (NSString *)maxString;
}
/*
 3.将字符串后移(10分)
 将字符串向右移动指定位数,首尾循环
 
 如:string传入@"welcometoqianfeng", bits传入4
 
 返回:@"fengwelcometoqian"
 */

-(NSString *)displacemetString:(NSString *)string forBits:(NSUInteger)bits {

    NSMutableString *str = [NSMutableString new];
    
    [str appendString:[NSString stringWithString:[string substringFromIndex:[string length] - bits]]];
    [str appendString:[NSString stringWithString:[string substringToIndex:[string length] - bits]]];
    
    return  (NSString *)str;

}
/*
 4.找出出现最多的字母 (10分)
 找出字符串中出现次数最多的字母,将该字母和字母出现的次数拼接成一个新字符串
 返回新字符串。
 
 传入:@"WelcomeToQianfeng"
 
 返回:@"e3"
 */
- (NSString *)maxTimesCharacterOfString:(NSString *)string {
    
    int maxCount = 1;
    
    int count = 1;
    
    NSUInteger maxIndex = 0;
    
    NSMutableString *string1 = [NSMutableString stringWithString:string];
    
    for (int i = 0; i < [string1 length]; i++) {
        if ([string1 characterAtIndex:i] == ' ') {
            continue;
        }
        
        count = 1;
        
        for (int j = i+1; j < [string1 length]; j++) {
            if ([string1 characterAtIndex:i] == [string1 characterAtIndex:j]) {
                count++;
                [string1 replaceCharactersInRange:NSMakeRange(j, 1) withString:@" "];
            }
        }
        if (count > maxCount) {
            maxCount  = count;
            maxIndex = i;
        }
    }
    
    return [NSString stringWithFormat:@"%c%d",[string1 characterAtIndex:maxIndex],maxCount];
}

/*
 5.将字符中单词用空格隔开(20分)
 已知传入的字符串中只有字母,每个单词的首字母大写,请将每个单词用空格隔开
 只保留第一个单词的首字母大写
 
 传入:@"HelloMyWorld"
 
 返回:@"Hello my world"
 */

- (NSString *)separateString:(NSString *)string {

    NSMutableString *str = [NSMutableString stringWithString:string];
    
    for (int i = 0; i < [str length]-1; i++) {
        unichar ch = [str characterAtIndex:i];
        unichar ch1 = [str characterAtIndex:i+1];
        if ((ch >= 'a' && ch <= 'z') && (ch1 >= 'A' && ch1 <= 'Z')) {
            [str insertString:@" " atIndex:i+1];
        }
    }
    return (NSString *)str;
    
}
/*
 6.字符串交错穿插(20分)
 已知两个字符串只由字母和空格组成,但两个字符串中包含的单词数不一定相等
 将两个字符串中的单词交错拼接到一起,当其中一个字符串的单词用尽,另一个
 字符串中剩余单词自然拼接在后面
 
 传中:@"Welcome to qianfeng"  @"hello my dear world"
 
 返回:@"Welcome hello to my qianfeng dear world"
 */
-(NSString *)staggerString:(NSString *)string1 withString:(NSString *)string2 {

    NSArray *arr1 = [string1 componentsSeparatedByString:@" "];
    NSArray *arr2 = [string2 componentsSeparatedByString:@" "];
    NSMutableArray *ary = [NSMutableArray new];
    int j = 0;
    for (int i = 0; i < arr1.count; i++) {
        [ary addObject:[NSString stringWithString:arr1[i]]];
        while(j < arr2.count) {
            [ary addObject: [NSString stringWithString:arr2[j]]];
            j++;
            break;
        }
    }
    while (j < arr2.count) {
        [ary addObject: [NSString stringWithString:arr2[j]]];
        j++;
    }
    return [NSString stringWithString:[ary componentsJoinedByString:@" "]];
}

/*
 7 //根据输入的内容打印(20分)
 
 //比如 [class print:5 blankString:@"+" flagString:@"#"];
 
 + + # + +
 
 + # + # +
 
 # + + + #
 
 + # + # +
 
 + + # + +
 
 */
+(void)print:(NSInteger)count blankString:(NSString*)blankString flagString:(NSString*)flagString {
    NSMutableString *str = [NSMutableString new];
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count; j++) {
            if ((i + j == (count -1)/2) || (j - i == (count -1)/2) || (i - j == (count -1)/2) || (i + j == 3*(count-1)/2)) {
                [str appendFormat:@"%@ ",flagString];
            }else {
                [str appendFormat:@"%@ ",blankString];
            }
        }
        [str appendFormat:@"\n"];
    }
    NSLog(@"\n%@",str);


}

+ (void)test {
    
    LWTest *test = [LWTest new];

    //第一题
    /*
    NSArray *arr = @[@"one",@"two",@"three"];
    NSLog(@"%@",[test symmetryArray:arr]);
*/
    //第二题
    /*
    NSArray *arr = @[@"adgh",@"sdghgdfs",@"dgkdg",@"dstpwrsadgdsg",@"gd"];
    NSLog(@"%@",[test longestStringInArray:arr]);
    */
    
    //第三题
    //NSLog(@"%@",[test displacemetString:@"welcometoqianfeng" forBits:4]);

    //第四题
    //NSLog(@"%@",[test maxTimesCharacterOfString:@"WelcomeToQianfeng"]);
    
    //第五题
    NSLog(@"%@",[test separateString:@"WhatAreYouFunckingDoing"]);
    
    //第六题
    
    //NSLog(@"\n%@",[test staggerString:@"Welcome to qianfeng haha haha hahah haha" withString:@"hello my dear world"]);
    
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值