Dinakar
Dinakar

Reputation: 1208

print reverse of a string

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 file:

#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 file

- (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]];
}

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.

Where am I going wrong?

Upvotes: 0

Views: 6882

Answers (6)

skjelland
skjelland

Reputation: 149

Using characterAtIndex doesn't work well with non-ascii characters.

- (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;
}

Upvotes: 0

dhani
dhani

Reputation: 1

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);

Upvotes: 0

pnizzle
pnizzle

Reputation: 6431

I revers my strings with a few lines of code. Rather late, but just putting it out there :)

- (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;
}

Upvotes: 0

Pierre
Pierre

Reputation: 2019

You insert the NSString object in your array. Its count is 1. You have to go from the end of the string to the beginning and append the character to a new string. You ca do it like this:

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

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

    return reversedString;
}

Upvotes: 9

Anurag
Anurag

Reputation: 141879

Another way to reverse strings is to use the reverse string enumerator.

- (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;
}

Upvotes: 5

Ishu
Ishu

Reputation: 12787

use this

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);

Upvotes: 3

Related Questions