🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of stevenhuey

stevenhuey's solution

to Acronym in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Test suite

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).


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/acronym/Acronym.m

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


Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

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


#import <XCTest/XCTest.h>

#if __has_include("AcronymExample.h")
# import "AcronymExample.h"
# else
# import "Acronym.h"

@interface AcronymTest : XCTestCase


@implementation AcronymTest

- (void)testAcronymAbbreviateTest1 {
    XCTAssertEqualObjects(@"PNG", [Acronym abbreviate:@"Portable Network Graphics"]);

- (void)testAcronymAbbreviateTest2 {
    XCTAssertEqualObjects(@"ROR", [Acronym abbreviate:@"Ruby on Rails"]);

- (void)testAcronymAbbreviateTest3 {
    XCTAssertEqualObjects(@"HTML", [Acronym abbreviate:@"HyperText Markup Language"]);

- (void)testAcronymAbbreviateTest4 {
    XCTAssertEqualObjects(@"FIFO", [Acronym abbreviate:@"First In, First Out"]);

- (void)testAcronymAbbreviateTest5 {
    XCTAssertEqualObjects(@"PHP", [Acronym abbreviate:@"PHP: Hypertext Preprocessor"]);

- (void)testAcronymAbbreviateTest6 {
    XCTAssertEqualObjects(@"CMOS", [Acronym abbreviate:@"Complementary metal-oxide semiconductor"]);

//  Acronym.m
//  acronynm
//  Created by Steve Huey on 7/10/17.
//  Copyright © 2017 Steve Huey. All rights reserved.

#import "Acronym.h"

@interface NSString (Acronym)
@property (readonly) NSString* capitalizeFirstLetter;

@implementation NSString (Acronym)
- (NSString *)capitalizeFirstLetter
   NSString* firstLetter = [[self substringToIndex:1] uppercaseString];
   return [self stringByReplacingCharactersInRange:NSMakeRange(0, 1)

- (NSString*)firstCharacter
   if (self.length > 0) {
      return [NSString stringWithFormat:@"%c", [self characterAtIndex:0]];
   } else {
      return @"";

- (BOOL)isAcronym
   return [[self uppercaseString] isEqualToString:self];

- (NSString*)uppercaseCharacters
   NSMutableString* result = [[NSMutableString alloc] initWithCapacity:0];
   for (NSUInteger index = 0; index < self.length; ++index) {
      unichar c = [self characterAtIndex:index];
      if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c]) {
         [result appendFormat:@"%c", c];
   return [NSString stringWithString:result];


@interface Acronym ()
+ (NSCharacterSet*)validCharacters;
+ (NSString*)replaceInvalidCharactersWithSpace:(NSString*)input;

@implementation Acronym
+ (NSString*)abbreviate:(NSString*)input
   NSString* filteredInput = [[self class] replaceInvalidCharactersWithSpace:input];
   NSMutableString* acronym = [[NSMutableString alloc] initWithCapacity:0];
   NSArray* components = [filteredInput componentsSeparatedByCharactersInSet:
                          [NSCharacterSet whitespaceCharacterSet]];
   for (NSString* word in components) {
      if (word.length > 0) {
         if ([word isAcronym]) {
            [acronym appendString:[word firstCharacter]];
         else {
            [acronym appendString:[[word capitalizeFirstLetter] uppercaseCharacters]];
   return [acronym uppercaseString];

+ (NSCharacterSet*)validCharacters
   static NSMutableCharacterSet* validCharacters = nil;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
      validCharacters = [[NSMutableCharacterSet alloc] init];
      [validCharacters formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];
      [validCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
   return validCharacters;

+ (NSString*)replaceInvalidCharactersWithSpace:(NSString*)input
   NSCharacterSet* validCharacters = [[self class] validCharacters];
   NSMutableString* filteredInput = [[NSMutableString alloc] initWithCapacity:input.length];
   for (NSUInteger index = 0; index < input.length; ++index) {
      unichar c = [input characterAtIndex:index];
      if ([validCharacters characterIsMember:c])
         [filteredInput appendFormat:@"%c", c];
      else {
         [filteredInput appendString:@" "];

   return [NSString stringWithString:filteredInput];

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?