OC中把字符串倒叙,且每个单词正序, 并计算单词数,两个单词间空格多少不影响结果

1 OC中把字符串倒叙,且每个单词正序, 并计算单词数,两个单词间空格多少不影响结果@”hello world how are you”,

代码如下
.m

@interface ViewController ()
@property (nonatomic , strong )NSString *demo ;//单个单词
@property (nonatomic , strong )NSString *resutStr;
@end

@implementation ViewController


//懒加载
-(NSString *)resutStr{
     if (_resutStr == nil) {
        _resutStr = [NSString string];
    }
    return _resutStr;
}

-(NSString *)demo{
      if (_demo == nil) {
        _demo = [NSString string];
    }
    return _demo;
}





- (void)viewDidLoad {
    [super viewDidLoad];

    int  n  = 1 ; //总单词数
    NSString *strs = @"hello world  how are you";
    NSLog(@"%@",strs);
    strs  =  [strs stringByAppendingString:@" "];//长度加1


    for (NSInteger i = strs.length - 1 ; i  >= 0 ; i--) {
        NSRange range = NSMakeRange(i, 1);
        NSString *str  =   [strs substringWithRange:range];

         //将几个字母组成的单词倒叙(两次倒叙为正)
        if ([str  isEqual: @" "]) {
            for (NSInteger m = self.demo.length - 1 ; m >= 0; m--) {
                NSRange rangeDemo = NSMakeRange(m, 1);
                NSString *strDemo  =   [self.demo substringWithRange:rangeDemo];
                //  NSLog(@"%@", strDemo);
                //记录输出
                self.resutStr =   [self.resutStr stringByAppendingString:strDemo];
            }
            //只有同时满足下列两个条件才是一个单词
            if ( (![self.demo isEqual:@" "]) && (![self.demo isEqual:@""]) ) {
                n = n +1 ;
            }
              self.demo = nil;
            }

    //将几个字母组成单词,
        self.demo =   [self.demo stringByAppendingString:str];



    }

    //第一个单词不会打印, 另外打印
    for (NSInteger m = self.demo.length - 1 ; m >= 0; m--) {
        NSRange rangeDemo = NSMakeRange(m, 1);
        NSString *strDemo  =   [self.demo substringWithRange:rangeDemo];
        if (![strDemo isEqual:@" "]) {
            // NSLog(@"%@", strDemo);
            self.resutStr =    [self.resutStr stringByAppendingString:strDemo];

        }

    }

    NSLog(@"%@\n 总单词数%d",self.resutStr,n);


}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值