打印字符串的反转

时间:2011-02-14 06:53:15

标签: iphone objective-c ipad

我正在编写一个程序,它在文本字段中打印字符串的反转,从另一个文本字段中获取输入文本。在将文本输入到一个文本字段后按Enter键时,结果(反向)应显示在另一个文本字段中。

我试过这样,但结果很奇怪。

.h文件:

#import <UIKit/UIKit.h>


@interface reverseVC : UIViewController {

    UITextField *textEntered;
    UITextField *textDisplay;

}
@property (nonatomic,retain) IBOutlet UITextField *textDisplay;
@property (nonatomic,retain) IBOutlet UITextField *textEntered;

- (NSMutableArray*) stringReverse;
- (IBAction) enter;

@end

.m文件

- (NSMutableArray *) stringReverse
{

    NSString *value = textEntered.text;
    NSArray *tempArray = [[[NSArray alloc] initWithObjects:value,nil] autorelease];

    NSMutableArray *arr = [[[NSMutableArray alloc] init] autorelease];


    for (int i=[tempArray count]-1; i>=0; i--)
    {
        [arr addObject:[tempArray objectAtIndex:i]];
        NSLog(@"the object is:%c",arr);
    }

    return arr;
}
-(IBAction)enter
{
   textDisplay.text = [NSString stringWithFormat:@"%c",[self stringReverse]];
}

之前在数组初始化中放置nil和autorelease之前会收到SIG_ABT和EXE_BAD_ACCESS之类的警告。现在,警告已经解决,但结果却不尽如人意。

我哪里错了?

6 个答案:

答案 0 :(得分:9)

您在阵列中插入NSString对象。它的计数是1.你必须从字符串的末尾到开头,并将字符附加到一个新的字符串。你可以这样做:

-(NSString*)reverseString:(NSString*)string {
    NSMutableString *reversedString;
    int length = [string length];
    reversedString = [NSMutableString stringWithCapacity:length];

    while (length--) {
            [reversedString appendFormat:@"%C", [string characterAtIndex:length]];
    }

    return reversedString;
}

答案 1 :(得分:5)

另一种反向字符串的方法是使用反向字符串枚举器。

- (NSString *)reverseString:(NSString *)string {
    NSMutableString *reversedString = [[NSMutableString alloc] init];
    NSRange fullRange = [string rangeOfString:string];
    NSStringEnumerationOptions enumerationOptions = (NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences);
    [string enumerateSubstringsInRange:fullRange options:enumerationOptions usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [reversedString appendString:substring];
    }];
    return reversedString;
}

答案 2 :(得分:3)

使用此

NSString *str=textEntered.text;//
    NSMutableArray *temp=[[NSMutableArray alloc] init];
    for(int i=0;i<[str length];i++)
    {
        [temp addObject:[NSString stringWithFormat:@"%c",[str characterAtIndex:i]]];
    }
    temp = [NSMutableArray arrayWithArray:[[temp reverseObjectEnumerator] allObjects]];
    NSString *reverseString=@"";
    for(int i=0;i<[temp count];i++)
    {
        reverseString=[NSString stringWithFormat:@"%@%@",reverseString,[temp objectAtIndex:i]];
    }
    NSLog(@"%@",reverseString);

答案 3 :(得分:0)

我用几行代码来反转我的字符串。相当晚,但只是把它放在那里:)

- (NSString*) reverseMyString:(NSString*)theString
{
    NSString *final = @"";
    for(int i = theString.length-1; i>=0; i--)
        final = [NSString stringWithFormat:@"%@%c", final, [theString characterAtIndex:i]];
    return final;
}

答案 4 :(得分:0)

NSString *str = @"hello";
int length = [str length];

NSLog(@"%d",length);

NSMutableString *rev = [[NSMutableString alloc] initWithCapacity:[str length]];
while (length > 0) {
    --length;
    [rev appendString:[NSString stringWithFormat:@"%c", [str characterAtIndex:length]]];
}

NSLog(@"%@", rev);

答案 5 :(得分:0)

使用characterAtIndex对非ascii字符不起作用。

- (NSString *)reverseMyString:(NSString *)string {
    NSString *output = [[NSString alloc] init];
    for(int i = string.length-1; i>=0; i--) {
        output = [output stringByAppendingString:[string substringWithRange:NSMakeRange(i, 1)]];
    }

    return output;
}