Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of 6401

6401's solution

to Reverse String in the Objective-C Track

Published at Feb 06 2020 · 0 comments
Instructions
Test suite
Solution

Reverse a string.

For example: input: "cool" output: "looc"

Setup

There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/robot-name/RobotName.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.

Source

A debugging session with Paul Blackwell at gSchool. http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ReverseStringTest.m

#import <XCTest/XCTest.h>

#if __has_include("ReverseStringExample.h")
# import "ReverseStringExample.h"
# else
# import "ReverseString.h"
#endif

@interface ReverseStringTest : XCTestCase

@end

@implementation ReverseStringTest

-(void)testReverseEmptyString {
    
    NSString *inputString = [NSString string];
    NSString *expectedOutput = [NSString string];
    XCTAssertEqualObjects(expectedOutput, [inputString reverseString]);
}

-(void)testReverseWord {
    
    NSString *inputString = @"robot";
    NSString *expectedOutput = @"tobor";
    XCTAssertEqualObjects(expectedOutput,[inputString reverseString]);
}

-(void)testReverseCapitalisedWord {
    
    NSString *inputString = @"Ramen";
    NSString *expectedOutput = @"nemaR";
    XCTAssertEqualObjects(expectedOutput, [inputString reverseString]);
    
}

-(void)testReverseSentenceWithPunctuation {
    
    NSString *inputString = @"I'm hungry!";
    NSString *expectedOutput = @"!yrgnuh m'I";
    XCTAssertEqualObjects(expectedOutput, [inputString reverseString]);
}

-(void)testReversePalindrome {
    
    NSString *inputString = @"racecar";
    NSString *expectedOutput = @"racecar";
    XCTAssertEqualObjects(expectedOutput, [inputString reverseString]);
}

@end

ReverseString/ReverseString_Tests/ReverseString.m

//
//  ReverseString.m
//  ReverseString_Tests
//
//  Created by Сергей Пупчик on 06/02/2020.
//  Copyright © 2020 Сергей Пупчик. All rights reserved.
//

#import "ReverseString.h"

@implementation NSString(reverseString)
-(NSString*)reverseString{
    NSMutableString *str = [[NSMutableString alloc]init];
    for (int k=(int)[self length]-1;k>=0;k--){
    char c= [self characterAtIndex:k];
        [str appendFormat:@"%c",c];
    }
    return str;
}
@end

ReverseString/ReverseString_Tests/ReverseString.h

//
//  ReverseString.h
//  ReverseString_Tests
//
//  Created by Сергей Пупчик on 06/02/2020.
//  Copyright © 2020 Сергей Пупчик. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSString(reverseString){
//    NSString *str;
}
-(NSString*)reverseString;
@end

NS_ASSUME_NONNULL_END

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?