打印字符串的反转 [英] print reverse of a string
本文介绍了打印字符串的反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序,它在文本字段中打印字符串的反转,从另一个文本字段中获取输入文本。当我在一个文本字段中输入文本后按Enter键时,结果(反向)应显示在另一个文本字段中。
I am writing a program which prints the reverse of a string in a textfield, taking input text from the other textfield. When I press enter after entering text into one textfield the result(reverse) should be dispalyed in the other text field.
我试过这样的,但结果很奇怪。
I have tried like this, but am getting weird results.
.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等警告。现在警告已经解决但结果出乎意料。
Earlier got warnings like SIG_ABT and EXE_BAD_ACCESS before placing nil and autorelease in array initialisations. Now the warnings are solved but results undesiredly.
我哪里出错?
推荐答案
使用此
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);
这篇关于打印字符串的反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文