Thursday, August 11, 2011

Objective-C: String reverting

Hi!


Here's a very simple piece of code  to revert/twist/turn a string:



#import <UIKit/UIKit.h>

@interface StringTurnerViewController : UIViewController{
    
    IBOutlet UIButton* BUTTON;
    IBOutlet UITextField* INSERT_TEXT_FIELD;
    IBOutlet UITextField* RESULT_TEXT_FIELD;
    
}

-(IBAction)revertTheString:(id)sender;
-(int)countStringLenght:(NSString *)string;


@end

#import "StringTurnerViewController.h"

@implementation StringTurnerViewController


-(IBAction)revertTheString:(id)sender{
            
    NSString* STRING_TO_TRANSFORM = INSERT_TEXT_FIELD.text;
    NSString* STRING_WHO_WILL_RECEIVE_CHAR_BY_CHAR;
    NSMutableString* STRING_TO_DISPLAY = [NSMutableString string];
    NSMutableString* MUTABLE_STRING_TO_TRANSFORM = [NSMutableString stringWithString:STRING_TO_TRANSFORM];
    int STRING_SIZE;
    
    STRING_SIZE = [self countStringLenght:STRING_TO_TRANSFORM];
    int x;
    for (x=0; x<=[STRING_TO_TRANSFORM length]; x++){
        STRING_SIZE--;
        if(x <= [STRING_TO_TRANSFORM length]-1){
            STRING_WHO_WILL_RECEIVE_CHAR_BY_CHAR = [MUTABLE_STRING_TO_TRANSFORM substringWithRange:NSMakeRange(STRING_SIZE, 1)];
            [STRING_TO_DISPLAY appendString:STRING_WHO_WILL_RECEIVE_CHAR_BY_CHAR];
        }
    }
    
    RESULT_TEXT_FIELD.text = STRING_TO_DISPLAY;
    
}

-(int)countStringLenght:(NSString *)string{
    
    int stringLength = 0;    
    for(int i=0; i<=[string length]; i++){
        stringLength = i;
    }
    return  stringLength;
}

...

@end

Have a good day, pals.


No comments:

Post a Comment

Tweet