zoukankan      html  css  js  c++  java
  • OC-数组类-几道练习

    3.有两个字符串@"I am a good girl";@"You are a bad boy"; 将这两个字符串单词,交叠,形成系的字符串 @"I You am are a a good bad girl boy"

     1 int main(int argc, const char * argv[]) {
     2     @autoreleasepool {
     3          char str1[100] = {};
     4         char str2[100] = {};
     5         //按格式输入:%[^
    ] 解释:表示除了'
    '所有字符都被视为字符串的一部分,】'
    ':读取到'
    '时,为第二个字符串输入
     6         scanf("%[^
    ]
    %[^
    ]",str1,str2);
     7         NSString *string1 = [NSString stringWithUTF8String:str1];
     8         NSString *string2 = [NSString stringWithUTF8String:str2];
     9         
    10         NSLog(@"string1 = %@",string1);
    11         NSLog(@"string2 = %@",string2);
    12         
    13         NSArray *array1 = [string1 componentsSeparatedByString:@" "];
    14         NSArray *array2 = [string2 componentsSeparatedByString:@" "];
    15         NSUInteger len1 = [array1 count];
    16         NSUInteger len2 = [array2 count];
    17         NSArray *array = [[NSArray alloc]init];
    18         NSUInteger len = len1<len2?len1:len2;
    19         for (int i=0; i<len; i++) {
    20             array = [array arrayByAddingObject:[array1 objectAtIndex:i]];
    21             array = [array arrayByAddingObject:[array2 objectAtIndex:i]];
    22         }
    23         
    24         //获取比较长的字符串的剩余子串
    25         NSArray *subArray = [NSArray array];
    26         if (len1 > len2) {
    27             subArray = [array1 subarrayWithRange:NSMakeRange(len, len1-len)];
    28         }else{
    29             subArray = [array2 subarrayWithRange:NSMakeRange(len, len2-len)];
    30         }
    31         
    32         NSArray *resArray = [array arrayByAddingObjectsFromArray:subArray];
    33         NSString *resString = [resArray componentsJoinedByString:@" "];
    34         NSLog(@"%@",resString);   
    35     }
    36 }    

     2.对称字符串

     传入一个字符串,将这个字符串组合成一个新的对称字符串返回

     例如:

     传入:@"I love you Mery"

     返回:@"I love you Mery Mery you love I"

     1 int main(int argc, const char * argv[]) {
     2     @autoreleasepool {
     3         
     4         //以字符串整体进行分割
     5         NSString *src = @"I love you Mery";
     6         NSArray *array = [src componentsSeparatedByString:@" "];
     7         NSArray *retArray = [[NSArray alloc] initWithArray:array];
     8         //1.method:第一种for循环遍历
     9         /*
    10         NSUInteger len = [array count];
    11         for (NSInteger i=len-1; i>=0; i--) {
    12             retArray = [retArray arrayByAddingObject:[array objectAtIndex:i]];
    13         }
    14         NSString *resString = [retArray componentsJoinedByString:@" "];
    15         NSLog(@"%@",resString);
    16          */
    17         //2.第二种枚举器遍历
    18         /*
    19         NSEnumerator *reverseEnumerator = [[NSEnumerator alloc] init];
    20         reverseEnumerator = [array reverseObjectEnumerator];
    21         id obj = nil;
    22         while (obj = [reverseEnumerator nextObject]) {
    23             retArray = [retArray arrayByAddingObject:obj];
    24         }
    25         NSString *resString = [retArray componentsJoinedByString:@" "];
    26         NSLog(@"%@",resString);
    27          */
    28     }
    29 }
  • 相关阅读:
    华为交换机端口安全
    华为路由交换命令收集
    Java使用JodaTime处理时间
    Java遍历Map对象的四种方式
    zabbix-2.4.8-1添加MySQL状态监控
    zabbix-2.4.8-1添加tcp状态监控
    zabbix-2.4.8-1添加nginx状态监控
    我的第四个Python小程序
    我的第三个Python小程序
    我的第二个Python小程序
  • 原文地址:https://www.cnblogs.com/BeyondAverage0908/p/4570033.html
Copyright © 2011-2022 走看看