Reverse String in Objective C

Published on 14 October 2019 (Updated: 16 December 2023)

Welcome to the Reverse String in Objective C page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#import <Foundation/Foundation.h>

@interface StringHelper:NSObject
- (NSString *) reverseString:(NSString *)stringToReverse;
@end

@implementation StringHelper 
- (NSString *) reverseString:(NSString *)stringToReverse {
    NSMutableString* result = [NSMutableString string];
    NSInteger index = [stringToReverse length];
    while (index > 0) {
        index--;
        NSRange range = NSMakeRange(index, 1);
        [result appendString:[stringToReverse substringWithRange:range]];
    }
    return result;
}

@end

int main (int argc, const char *argv[]){
    NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
    if (argc >= 2){
        NSString *userInput =[NSString stringWithUTF8String:argv[1]];
        if([userInput length] > 0){
            StringHelper* helper = [[StringHelper alloc] init];
            printf("%s\n", [[helper reverseString: userInput] UTF8String]);
        }
    }
    [pool drain];
    return 0;
}

Reverse String in Objective C was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.