OC 习题2的答案

/*1. 输入一段字符串,已知字符串只由字母和空格构成,统计其中的单词个数 (10分)
 比如:传入 @"welcom to zhongguo"  返回:3
 */
- (NSUInteger)countOfWordInString:(NSString *)str {

    NSArray *ary = [str componentsSeparatedByString:@" "];
    [(NSMutableArray *)ary removeObject:@""];
    return ary.count;
}


/*
 2.已知字符串中有各种字符,返回出现次数最多的字符。如果某些字符个数相同,返回ASIIC码最大的那个。(15分)
 */
- (unichar) mostCharacterInString:(NSString *)str {
    int count = 1;
    int maxCount = 1;
    NSUInteger maxIndex = 0;
    NSMutableString *str1 = [NSMutableString stringWithString:str];
    //先统计每个字符出现的个数
    for (int i = 0; i < [str1 length]; i ++ ) {
        if ([str1 characterAtIndex:i] ==  ' ') {
            continue;
        }
        count = 1;
        for (int j = i+1; j < [str1 length]; j++) {
            if ([str1 characterAtIndex:i] == [str1 characterAtIndex:j] ) {
                count++;
                [str1 replaceCharactersInRange:NSMakeRange(j, 1) withString:@" "];
            }
        }
        //将个数与最大值比较,大于则赋值给最大,等于则比较ASCII大小
        if (count > maxCount) {
            maxCount = count;
            maxIndex = i;
        }
        if (count == maxCount) {
            if ([str1 characterAtIndex:i] > [str1 characterAtIndex:maxIndex]) {
                maxIndex = i;
            }
        }
    }

    return [str1 characterAtIndex:maxIndex];
}

/*
 3.传入一个字符串,判断字符串是否是合法邮箱,是,返回YES,不是返回NO。(15分)
 mail@1000phone.com    是
 $mail@1000phone.com   不是
 */
- (BOOL) isMailString:(NSString *)str {
    NSArray *ary = [str componentsSeparatedByString:@"@"];
    NSMutableString *str1 = [NSMutableString stringWithString:ary[1]];
    [str1 insertString:@"@" atIndex:0];
    //先判断域名是否合法,不合法直接返回NO
    if (!([str1 compare:@"@1000Phone.com"])) {
        return NO;
    }
    int flag = 0;
    NSString *str2 = [NSString stringWithString:ary[0]];
    //判断域名前面的是否合法,字符串只能由字母数字和下划线组成
    for (int i = 0; i < [str2 length]; i++) {
        flag = 0;
        unichar ch = [str2 characterAtIndex:i];
        if (!( (ch >= '0' && ch <= '9') || (ch == '_') || ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')))){
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        return NO;
    }else{
        return YES;
    }
    
    
    
}
/*
 4.把字符串中的字母大小写反转(10分)
 将字符数组s中每个字母大写变成小写,小写变成大写,其他字符不动。
 */

- (NSString *)reverseString:(NSString *)str {
    NSMutableString *str1 = [NSMutableString stringWithString:str];
    for (int i = 0; i < [str1 length]; i++) {
        unichar ch = [str1 characterAtIndex:i];
        if (ch >= 'a' && ch <= 'z') {
            [str1 replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch - 32]];
        }else if (ch >= 'A' && ch <= 'Z'){
            [str1 replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch + 32]];
        }
    }
    return (NSString *)str1;
}
/*
 5.为NSArray添加方法,返回一个字典,将数组中的索引序号转换成字符串作为key,将元素内容转换为值。(15分)
 如:数组元素为@[@"Zero", @"One", @"Two", @"Three"]
 返回:{@"0":@"Zero", @"1":@"One", @"2":@"Two", @"3":@"Three"}
 */

- (NSDictionary *)dictionaryFromArray:(NSArray *)array {
    NSMutableDictionary *dic = [NSMutableDictionary new];
    NSMutableString *str = [NSMutableString new];
    for (int i = 0; i < array.count; i++) {
        [str appendFormat:@"%d",i];
        [dic setValue:array[i] forKey:str];
        [str setString:@""];
    }
    return dic;
    
}

/*
 6.将字符串中单词后移(15分)
 已知字符串中的单词以空格隔开,将字符串向右移动指定单词数,首尾循环
 如:string传入@"Hi man welcome to zhongguo", step传入2
 返回:@"to zhongguo Hi man welcome"
 */

- (NSString *)string:(NSString *)originalStr righMoveStep:(NSUInteger)step {

    NSString *str = [NSString new];
    NSArray *ary = [originalStr componentsSeparatedByString:@" "];
    [(NSMutableArray *)ary removeObject:@""];
    NSMutableArray *ary1 = [NSMutableArray new];
    for (int j = (int)(ary.count-step); j < ary.count; j++) {
        [ary1 addObject:ary[j]];
    }
    for (int i = 0; i < ary.count-step; i++) {
        [ary1 addObject:ary[i]];
    }
    str = [ary1 componentsJoinedByString:@" "];
    return str;
}

/*
 7.根据输入的内容打印(20分)
 比如 [obj print:@"12345"];
 
 12345
 2
 3
 4
 12345
 */

- (void)printString:(NSString *)string {
    NSMutableString *str = [NSMutableString new];
    for (int i = 0; i < [string length]; i++) {
        for (int j = 0; j < [string length]; j++) {
            if (i == 0 || i == [string length] - 1) {
                [str appendFormat:@"%c",[string characterAtIndex:j]];
            }else if(j == 0 ){
                [str appendFormat:@"%c",[string characterAtIndex:i]];
            }else{
                [str appendFormat:@" "];
            }
        }
        [str appendFormat:@"\n"];
    }
    
    NSLog(@"\n%@",str);
}


















+ (void)test {
    LWTest *Test = [LWTest new];
//第一题
    
    char s[100];
    gets(s);
    NSString *str = [NSString stringWithUTF8String:s];
    
    NSLog(@"%lu",[Test countOfWordInString:str]);
    
    
    //第二题
    //char s[100];
    //gets(s);
    //NSString *str = [NSString stringWithUTF8String:s];
    
    //NSLog(@"%c",[Test mostCharacterInString:str]);
    
    //第三题
    NSLog(@"%d",[Test isMailString:str]);
    
    //第四题
   // NSLog(@"%@",[Test reverseString:str]);
    //第五题
    /*
    NSArray *arry = @[@"Zero", @"One", @"Two", @"Three"];
    NSLog(@"%@",[Test dictionaryFromArray:arry]);
    */
    //第六题
    /*
    NSString *str = @"Hi man welcome to qianfeng";
    NSLog(@"\n%@",[Test string:str righMoveStep:2]);
    */
    //第七题
   // NSString *str = @"1234567";
   // [Test printString:str];
    
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值