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

EggmanQi's solution

to Acronym in the Objective-C Track

Published at Apr 25 2019 · 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
//  exercismOC
//  Created by eggman qi on 2019/4/24.
//  Copyright © 2019 EBrainStudio. All rights reserved.

#import "Acronym.h"

@interface Acronym ()

@property (nonatomic, strong) NSPredicate *p_onlyStr;
@property (nonatomic, strong) NSPredicate *p_onlyUpperCaseStr;


@implementation Acronym

+ (NSString *)abbreviate:(NSString *_Nullable)str
	Acronym *acronym = [[Acronym alloc] init];
	NSArray *resultArr = [acronym wordsArr:str];
	return [resultArr componentsJoinedByString:@""];

- (id)init
	if (self = [super init]) {
		NSString *regex_1 = @"^[a-zA-Z]*$";
		_p_onlyStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex_1];
		NSString *regex_2 = @"^[A-Z]*$";
		_p_onlyUpperCaseStr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex_2];
	return self;

- (NSArray *)wordsArr:(NSString *)str
	NSMutableArray *tempArr = [NSMutableArray array];
	BOOL isMatch = NO; 
	for (int i=0; i<str.length; i++) {
		NSString *subStr = [str substringWithRange:NSMakeRange(i, 1)];
		if ([self isOnlyStr:subStr]) {
			if (isMatch) {
				if (i>0) {
					NSString *preStr = [str substringWithRange:NSMakeRange(i-1, 1)];
					if ([self isUpperCaseStr:preStr]==NO && 
						[self isUpperCaseStr:subStr]==YES) {
						[tempArr addObject:subStr];
			}else {
				[tempArr addObject:[subStr uppercaseString]];
				isMatch = YES;
		}else {
			isMatch = NO;
	return tempArr.copy;

- (BOOL)isOnlyStr:(NSString *)str 
	return [_p_onlyStr evaluateWithObject:str];

- (BOOL)isUpperCaseStr:(NSString *)str
	return [_p_onlyUpperCaseStr evaluateWithObject:str];


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?