OC习题和答案2

/*
 1.传入一个字符串,已知字符串只由字母组成,将其中的大写字母转换为小写,小写转换为大写,返回转换后的字符串(15分)
 如传入:@"GOODgoodSTUDY",返回@"goodGOODstudy"
 */
- (NSString *)upperExchangeLower:(NSString *)str {
    NSMutableString *string = [NSMutableString stringWithString:str];
    for (int i = 0; i < [string length]; i++) {
        unichar ch = [string characterAtIndex:i];
        if (ch >= 'a' && ch <= 'z') {
            [string replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch-32]];
        }else{
            [string replaceCharactersInRange:NSMakeRange(i , 1) withString:[NSString stringWithFormat:@"%c",ch + 32]];
        }
    }

    return (NSString *)string;
}

/*
 2.实现函数,查找子串出现次数,返回字符串s中出现子串substring的次数,若没有出现返回0(15分)。
 如传入:@"drink your drink,don't drink others drink"和@"drink",返回4
 */
- (int)countOfSubstrin:(NSString *)s and:(NSString *)substring {
    int count = 0;
    NSMutableArray *ary = [NSMutableArray new];
    ary = (NSMutableArray *)[s componentsSeparatedByString:@" "];
    [ary removeObject:@""];
    for (int i = 0; i < ary.count; i++) {
        
        
    
    
    }
        return count;
    
}
/*
 3.(15分)
 函数功能:传入一个数组,返回数组中第二大数。已知数组中的对象都是数字构成的字符串,且任意两元素不相等。
 如传入:@[@"1",@"2",@"3",@"4"],返回@"3"
 */
- (NSString *)secondMaxItem:(NSArray *)ary {

    NSString *max = [ary[0] intValue] > [ary[1] intValue]?ary[0]:ary[1];
    NSString *secondMax = [ary[0] intValue] > [ary[1] intValue]?ary[1]:ary[2];
    for (int i = 2; i < ary.count; i++) {
        if ([ary[i] intValue] > [max intValue]) {
            secondMax = [NSString stringWithString:max];
            max = [NSString stringWithString:ary[i]];
        }else if([ary[i] intValue] > [secondMax intValue]){
            secondMax = [NSString stringWithString:ary[i]];
        }
    }
    return secondMax;
}

/*
 4.将字符串后移(15分)
 将字符串向右移动指定位数,首尾循环
 如:string传入"welcometobeijing", bits传入4
 返回:jingwelcometobei
 */
- (NSString *)displacemetString:(NSString *)string and:(int) bits {
    NSMutableString *str = [NSMutableString new];
    for (int i =(int) [string length] - bits; i < [string length]; i++) {
        [str appendFormat:@"%c",[string characterAtIndex:i]];
    }
    
    for (int i = 0; i < [string length] - bits; i++) {
        [str appendFormat:@"%c",[string characterAtIndex:i]];
    }

    return (NSString *)str;
}
/*
 5.已知字符串只由大写或小写英语字母组合,传入一个字符串,打印出现
 最多的字母,如果某些字母个数相同,打印ASIIC码最大的那个。(15分)
 如传入:@"Lifeislikeridingabicycletokeepyourblanceyoumustkeepmoving",打印: e
 */
- (void) mostLetterInString:(NSString *)str {
    NSMutableString *str1 = [NSMutableString stringWithString:str];
    int count = 1;
    int maxCount = 1;
    int maxIndex= 0;
    for (int i = 0; i < [str1 length]; i++) {
        unichar ch = [str1 characterAtIndex:i];
        count = 1;
        if (ch == ' ') {
            continue;
        }

        for (int j = i+1; j<[str1 length]; j++) {
            
            unichar ch1 = [str1 characterAtIndex:j];
            if (ch == ch1) {
                count++;
                [str1 replaceCharactersInRange:NSMakeRange(j, 1) withString:@" "];
            }
        }
        
        if (count > maxCount) {
            maxCount = count;
            maxIndex = i;
        }
        if(count == maxCount){
            if ([str1 characterAtIndex:i] > [str1 characterAtIndex:maxIndex]) {
                maxIndex = i;
            }
        }
        
    }
    
    NSLog(@"%c",[str1 characterAtIndex:maxIndex]);

}

/*
 6.排序并压缩字符串(15分)
 将字符串中的字符串按照ASCII从小到大的顺序排序,然后压缩
 压缩策略是将连续出现的字符转换成字符+次数的形式
 如传入:@"Lifeislikeridingabicycletokeepyourblanceyoumustkeepmoving"
 排序后(5分):@"Laabbcccdeeeeeeeefggiiiiiiikkklllmmnnnoooopprrssttuuuvyyy"
 压缩后(10分):
 @"La2b2c3de8fg2i7k3l3m2n3o4p2r2s2t2u3vy3"
 */
- (NSString *)sortAndCompress:(NSString *)string {
    NSMutableString *str = [NSMutableString stringWithString:string];
    int count = 1;
    NSMutableString *str0 = [NSMutableString new];
    //排序  冒泡法,对于可变字符串排序
    for (int i = 0; i < [str length]-1; i++) {
        for (int j = i+1; j < [str length]; j++) {
            if ([str characterAtIndex:i] > [str characterAtIndex:j]) {
                unichar temp = [str characterAtIndex:i];
                [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",[str characterAtIndex:j]]];
                [str replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",temp]];
            }
        }
    }
    //压缩过程
    for (int i = 0; i < [str length]; i+=count) {
        if ([str characterAtIndex:i] == ' ') {
            continue;
        }
        count = 1;
        for (int j = i+1; j < [str length]; j++) {
            if ([str characterAtIndex:i] == [str characterAtIndex:j]) {
                count++;
                [str replaceCharactersInRange:NSMakeRange(j, 1) withString:@" "];
            }
        }
        if (count > 1) {
            [str0 appendFormat:@"%c%d",[str characterAtIndex:i],count];
        }else{
            [str0 appendFormat:@"%c",[str characterAtIndex:i]];
    }
}
    return (NSString *)str0;

}

/*
 7.将字符串中单词按照出现次数(次数都不一样)降序排序,排序之后单词只出现一次,源字符串中单词用下划线连接,生成字符串也用下滑线连接(10分)
 如传入:@"good_good_study_good_study"
 返回:@"good_study"
 如传入:@"I_love_I_hate_love_love"
 返回:@"love_I_hate"
 */
- (NSString *)sortStringByNumberOfWordsFromString:(NSString *)str {
    NSMutableArray *ary = (NSMutableArray *)[str componentsSeparatedByString:@"_"];
    [ary removeObject:@""];
    NSMutableArray *arr = [NSMutableArray new];
    int count = 1;
    for (int i = 0; i < ary.count; i++) {
        if ([ary[i] isEqualToString:@" "]) {
            continue;
        }
        count = 1;
        for (int j = i+1; j<ary.count; j++) {
            if ([ary[i] isEqualToString:ary[j]]) {
                count++;
                [ary replaceObjectAtIndex:j withObject:@" "];
            }
        }
        [arr addObject:[NSString stringWithFormat:@"%d",count]];
    }
    [ary removeObject:@" "];
    for (int i = 0; i < arr.count; i++) {
        for (int j = i; j < arr.count-1; j++) {
            if ([arr[i] compare:arr[j]] == NSOrderedAscending) {
                [arr exchangeObjectAtIndex:i withObjectAtIndex:j];
                [ary exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    return [NSString stringWithString:[ary componentsJoinedByString:@"_"]];
}









+ (void)test {
    LWTest *test = [LWTest new];
    //第一题
    /*
NSString *str = @"GOODgoodSTUDY";
    
    NSLog(@"%@",[test upperExchangeLower:str]);
     */
    //第二题
    /*
    NSString *str =@"drink your drink,don't drink others drink";
    NSString *subString =@"drink";
    NSLog(@"%d",[test countOfSubstrin:str and:subString]);
    NSLog(@"%lu",[@"drink," length]);
     */
    
    //第三题
    /*
    NSArray *ary = @[@"1",@"2",@"3",@"4"];
    NSLog(@"%@",[test secondMaxItem:ary]);
    */
    //第四题
    /*
    NSString *str = @"welcometobeijing";
    NSLog(@"%@",[test displacemetString:str and:4]);
     */
    //第五题
    /*
    NSString *str = @"Lifeislikeridingabicycletokeepyourblanceyoumustkeepmoving";
    [test mostLetterInString:str];
    */
    //第六题
    /*
    NSString *str = @"Lifeislikeridingabicycletokeepyourblanceyoumustkeepmoving";
    NSLog(@"\n%@",[test sortAndCompress:str]);
    */
    //第七题
    NSString *str = @"good_good_study_good_study";
    NSLog(@"\n%@",[test sortStringByNumberOfWordsFromString:str]);
    
    
    
    
    
}

@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值