如何在不使用componentsSeparatedByString方法的情况下在目标c中打印NSString的反向?

时间:2013-05-09 10:23:37

标签: objective-c nsstring

我想创建一个反向string.suppose的方法我在方法中传递一个NSString“Welcome to Objective C”,该方法返回一个字符串的反向,如“C Objective to Welcome”而不是“C evitcejbO ot emocleW”不使用componentsSeparatedByString方法。 是否可以使用Objective c ..? 请帮忙。

6 个答案:

答案 0 :(得分:6)

您可以按字词枚举字符串。

NSString *string = @"Welcome to Objective-C!";

NSMutableArray *words = [NSMutableArray array];

[string enumerateLinguisticTagsInRange:NSMakeRange(0, [string length])
                                scheme:NSLinguisticTagSchemeTokenType
                               options:0
                           orthography:nil
                            usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                                [array addObject:[string substringWithRange:tokenRange]];
                            }];

NSMutableString *reverseString = [[NSMutableString alloc] init];

for (NSString *word in [words reverseObjectEnumerator]){
    [reverse appendString:word];
}

NSLog(@"%@", reverseString);

这将打印...

"!C-Objective to Welcome"

您可以更改选项以省略空格和内容......

答案 1 :(得分:2)

我使用以下方法在iOS中反转字符串

- (NSString *)reverseString:(NSString *)stringToReverse
{
    NSMutableString *reversedString = [NSMutableString stringWithCapacity:[stringToReverse length]];
    [stringToReverse enumerateSubstringsInRange:NSMakeRange(0, [stringToReverse length])
                                        options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
                                     usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            [reversedString appendString:substring];
    }];
    return reversedString;
}

答案 2 :(得分:1)

抱歉,我之前误解了你的问题。我是用一系列循环做的,我的答案比Fogmeister更麻烦,但我想试一试,看看能否做到。

NSString *str = @"This is a test";


    NSMutableArray *array = [[NSMutableArray alloc] init];

    for(int i = 0; i < [str length]; i++)
    {
        char sTest = [str characterAtIndex:i];
        if(sTest == ' ')
        {
            [array addObject:[NSNumber numberWithInt:i]];
        }
    }

    NSInteger iNext = [[array objectAtIndex:[array count]-1] integerValue];
    iNext+=1;

    if(iNext < [str length])
    {
       [array addObject:[NSNumber numberWithInt:iNext]]; 
    }

    NSMutableArray *wordArray = [[NSMutableArray alloc] init];

    for(int i = 0; i < [array count]; i++)
    {
        if (i == 0)
        {
            int num = [[array objectAtIndex:i] integerValue];
            NSString *s = [[str substringFromIndex:0] substringToIndex:num];
            [wordArray addObject:s];

        }
        else if(i == [array count]-1)
        {
            int prev = [[array objectAtIndex:i-1] integerValue]+1;
            int num =  [str length];
            NSString *s = [[str substringToIndex:num] substringFromIndex:prev];
            [wordArray addObject:s];
        }
        else
        {
            int prev = [[array objectAtIndex:i-1] integerValue]+1;
            int num = [[array objectAtIndex:i] integerValue];

            NSString *s = [[str substringToIndex:num] substringFromIndex:prev];
            [wordArray addObject:s];
        }
    }

    NSMutableArray *reverseArray = [[NSMutableArray alloc]init];
    for(int i = [wordArray count]-1; i >= 0; i--)
    {

        [reverseArray addObject:[wordArray objectAtIndex:i]];
    }
    NSLog(@"%@", reverseArray);

答案 3 :(得分:1)

在这里,我完成了用最少的循环次数替换字符。的log(n / 2)。

NSString  *string=@"Happy World";
NSInteger lenth=[string length];
NSInteger halfLength=[string length]/2;

for(int i=0;i<halfLength;i++)
{
   NSString *leftString=[NSString stringWithFormat:@"%c",[string characterAtIndex:i]];
   NSString *rightString=[NSString stringWithFormat:@"%c",[string characterAtIndex:(lenth-i-1)]];

   string= [string stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:rightString];
   string=[string stringByReplacingCharactersInRange:NSMakeRange((lenth-i-1), 1) withString:leftString];

}

NSLog(@"%@",string);

答案 4 :(得分:1)

试试这个,它按照你的期望完美运作,

通话功能: -

 [self reversedString:@"iOS"];

反转字符串功能: -

 -(void)reversedString :(NSString *)reversStr

 {    // reversStr is "iOS"

     NSMutableString *reversedString = [NSMutableString string];
     NSInteger charIndex = [reversStr length];
     while (charIndex > 0) {
     charIndex--;
     NSRange subStrRange = NSMakeRange(charIndex, 1);
    [reversedString appendString:[reversStr substringWithRange:subStrRange]];
  }
   NSLog(@"%@", reversedString); // outputs "SOi"
 }

希望所以这对某些人来说是有帮助的。

答案 5 :(得分:0)

没有API可以做到这一点,如果这就是你要求的。

您可以随时遍历字符串以查找空格(或标点符号,这取决于您的需要),识别单词并手动重新组合“反向”消息。