Reading Standard Input (StdIn) in Swift

Hello! Swifty Buddies. If you have tried out some of the challenges on Hackerrank using Swift as a preferred language, you might have came across this situation of reading input from StdIn because that is how the Test case inputs are provided on Hackerrank.

For most of the languages, you will get a boilerplate code and you just need to write your logic but as soon as you select swift as a language, you will get this:

import Foundation;

// Enter your code here

Yeah, that’s all you got 🙂

How to read Standard Input?

To read standard input in swift you will need to use readLine() function.

Ok, where to write code?

One option is to create MacOS –>Command Line Tool application but if you are used to only iOS applications, this will take some time to figure it out. Let us stick with Playgrounds only which is fun to work with for us.

But, readLine() always return nil in the playground. How do I test it? Don’t worry, I will come to that part. First, let us look at the logic that we will need to read inputs for Hackerrank.

Reading string

let string = readLine(strippingNewline: true)!

It is ok to use force wrap over here since on Hackerrank input values are always present. But, don’t do this in production code.

Reading Int

let number = Int(readLine(strippingNewline: true)!)!

Reading array of string

For inputs sample as below:

a b c d b

You can ready string array by splitting using the space character as below:

let stringArray = readLine(strippingNewline: true)!.characters
.split {$0 == ” “}
.map (String.init)

Writing to standard output

You can write the final output using the same way we print to console using print() function.

Example

Let us take one of the existing example from Hackerrank.

**Problem Description:**Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.

Sample Input:

cde  
abc

Write your solution in playground. Note that the playground will show error as it will encounter nil for readLine(). You can guard it or provide default value for coming up with the solution and can change it back to readLine() code when it is ready to paste it in Hackerrank’s editor.

Here is the solution which I came up with for this problem:

Testing

Now, to test it save it as Anagram.swift file at your preferred location. Go to that location on terminal and type:

swift Anagram.swift

Now, you are ready to give inputs and test it.

Voila!! You are ready to paste your code in Hackerrank now. Test with few different inputs and once you are ready, make sure to comment all the print logs except the final answer one and paste it in Hackerrank to run and test it.

This might get you through most of the challenges by writing in swift.

Let me know your feedbacks and what solution did you come up with for this problem? 🙂

Did you find this article valuable?

Support Javal Nanda by becoming a sponsor. Any amount is appreciated!