A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 15098037606 中级黑马   /  2015-4-20 16:07  /  571 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#import <Foundation/Foundation.h>
@interface NSString( XZ )// 方法声明
+ (void) zifuchuanfanzhuan:(NSMutableString *)s1;
+ (int) letterCount: (NSMutableString *)s2;
+ (NSMutableString *) qukongge:(NSMutableString *)s3;
@end

@implementation NSString( XZ )// 方法实现

+ (void) zifuchuanfanzhuan:(NSMutableString *)s1
{
    int changDu = (int)s1.length;

    //  将字符串最右端字符的下标储存在i中,遍历,直到字符串中间位置结束
    for (int i = (changDu - 1); i >= (changDu / 2); i--)
    {
        //  在遍历的同时储存要交换的两个字符的值
        char right = [s1 characterAtIndex:i];
        char left = [s1 characterAtIndex:(changDu - i - 1)];

        //  交换两个字符的值,同时将两个字符转换成字符串,方便后面的替换字符串操作
        NSString *rightNow = [[NSString alloc] initWithFormat:@"%c",left];
        NSString *leftNow = [[NSString alloc]initWithFormat:@"%c",right];

        //  将交换好的字符的字符串替换进原来的字符串中
        [s1 replaceCharactersInRange:NSMakeRange(i, 1) withString:rightNow];
        [s1 replaceCharactersInRange:NSMakeRange(changDu - i - 1, 1) withString:leftNow];
    }


+ (int) letterCount: (NSMutableString *)s2
{
        int  count = 0;
        for (int i=0 ;i<self.length;i++)
        {
            unichar  c = [self characterAtIndex :i];
            if(( c>='a'&& c<='z')||(c>='A'&& c<='Z'))//判断是否为英文字母
            {
                count++;
            }
        }
        return count;
    }

    + (NSMutableString *) qukongge:(NSString *)s3
    {
        NSMutableString *newS1 = [NSMutableString stringWithString:s3];
        NSMutableString *temp = nil ;
        for(int i =0; i < self.length; i++)
        {
            char c = [newS1  characterAtIndex:i]; // 获取当前位置的字符
            if(c == ' ')
            {
                temp = [newS1  deleteCharactersInRange:NSMakeRange(i, 1)];
            }
            else  break;
        }

        return temp;
    }

    @end

    int main()
    {
        NSMutableString *s1 = @"   123  ";
        NSMutableString *s2 = @"  5435abc54abc3AHJ5 ";
        NSMutableString *s3=@"  1235 45 ";

        NSMutableString *s1 = [NSString zifuchuanfanzhuan:s1]
        int  count = [NSString letterCount: s2] ;
        NSMutableString *s3 = [NSString qukongge: s3];
        NSLog(@"字符串反转后输出是%@", s1);
        NSLog(@"用类方法2后,字母个数是%d", count);
        NSLog(@"用类方法3去两端空格后,输出是 %@", s3);
        return 0;
    }

警告:method for definition  'qukongge:' not found            method  for definition 'letterCount:' not found 报错:use of declared identifier 'letterCount'哪位帮我修改一下?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马