Chapter 5: Strings

Swift Programming from Scratch

The book is updated for Swift 3 and the Swift Sandbox is integrated, making the exercises interactive. Read more about the book here.

  1. First Steps
  2. Conditionals
  3. Types
  4. Loops
  5. Strings
  6. Arrays
  7. Functions
  8. Recursion
  9. Closures
  10. Tuples & Enums
  11. Dictionaries

Chapter 5: Strings

 achievement5@2x

 

A string is an ordered collection of characters, such as "We ❤ Swift" or "eat, sleep, code, repeat!". In Swift strings are represented by the String type which is a collection of values of Character type.

Creating strings

You can include predefined String values within your code as string literals. A string literal is a fixed sequence of textual characters surrounded by a pair of double quotes (“”).

let aString = "Hello"

Take note of a special case of string, the empty string.

var emptyString = "" // empty string literal

You can create a new string by concatenating two strings using the + operator.

let newString = "Hello" + " swift lovers" // "Hello swift lovers"

String interpolation can also be used to combine strings. By using the \(<value>) syntax inside a string literal.

let bat = "BAT"
let man = "MAN"

// "BATMAN" - \(bat) will be replaced with "BAT" and \(man) with "MAN" 
let batman = "\(bat)\(man)" 

print("\(bat) + \(man) = \(batman)")
// "BAT + MAN = BATMAN"

Characters

In some cases you will want to work with the individual characters that make up the string. To do that you can use thefor-in syntax. A string exposes the list of characters with the characters property.

var someString = "this string has 29 characters"

for character in someString.characters {
    // to convert a Character into a String use string interpolation
    // you will need to do this in order to compare characters
    var characterAsString = "\(character)"


    print(characterAsString)
}

To see how many character a string has we can use the count property on the characters property of the string.

var string = "This string has 29 characters"
print(string.characters.count) // 29

Comparing

You can compare string using the same operators as numbers, but usually you only care about equality.

"We ❤ Swift" == "We ❤ Swift" // true
"We ❤ Swift" == "we ❤ swift" // false - string comparison is case sensitive

"We ❤ Swift" != "We ❤ Swift" // false 
"We ❤ Swift" != "we ❤ swift" // true

5.1 Full name

You are given the firstName and lastName of a user. Create a string variable called fullName that contains the full name of the user.

Example 1

Input:

var firstName = "Andrei"
var lastName = "Puni"

Output:

"Andrei Puni"

[collapse]
Example 2

Input:

var firstName = "Steve"
var lastName = "Jobs"

Output:

"Steve Jobs"

[collapse]
Hint

Use string concatenation or use string interpolation.

[collapse]
Solution 1

var firstName = "Andrei"
var lastName = "Puni"

var fullName = firstName + " " + lastName

[collapse]
Solution 2

var firstName = "Andrei"
var lastName = "Puni"

var fullName = "\(firstName) \(lastName)"

[collapse]

5.2 Sum

You are given two numbers a and b. Compute the sum of a and b and create a string stored in a variable namedformattedSum that contains the sum written like bellow:

For a = 2 and b = 5

formattedSum = "2 + 5 = 7"

For a = 12 and b = 19

formattedSum = "12 + 19 = 31"

Example 1

Input:

var a = 14
var b = 19

Expected values:

formattedSum = "14 + 19 = 31"

[collapse]
Example 2

Input:

var a = 12345
var b = 98765

Expected values:

formattedSum = "12345 + 98765 = 111110"

[collapse]
Hint

Use string interpolation.

[collapse]
Solution

var a = 14
var b = 23

var sum = a + b

var formattedSum = "\(a) + \(b) = \(sum)"

[collapse]

5.3 Replace

You are given a string stored in the variable aString. Create new string named replacedString that contains the characters of the original string with all the occurrences of the character "e" replaced by "*".

This content is restricted to buyers of Online Exercise Platform.

Example 1

Input:

var aString = "Replace the letter e with *"

Expected values:

replacedString = "R*plac* th* l*tt*r * with *"

[collapse]
Example 2

Input:

var aString = "Replace the letter e with *"

Expected values:

replacedString = "R*plac* th* l*tt*r * with *"

[collapse]
Example 3

Input:

var aString = "eeee eeee"

Expected values:

replacedString = "**** ****"

[collapse]
Example 4

Input:

var aString = "Did you know that Kathy is throwing a party tonight?"

Expected values:

replacedString = "Did you know that Kathy is throwing a party tonight?"

[collapse]
Hint

Create replacedString step by step by iterating over the characters ofaString

[collapse]
Solution

var aString = "Replace the letter e with *"

var replacedString = ""

for character in aString.characters {
    var char = "\(character)"
    if char == "e" {
        replacedString = replacedString + "*"
    } else {
        replacedString = replacedString + char
    }
}

[collapse]

5.4 Reverse

You are given a string stored in variable aString. Create a new string called reverse that contains the original string in reverse order. Print the reversed string.

"Hello" -> "olleH"
"We ❤ Swift" -> "tfiwS ❤ eW"

This content is restricted to buyers of Online Exercise Platform.

Example 1

Input:

var aString = "Hello"

Expected values:

reverse = "olleH"

Output:

"olleH"

[collapse]
Example 2

Input:

var aString = "We ❤ Swift"

Expected values:

reverse = "tfiwS ❤ eW"

Output:

"tfiwS ❤ eW"

[collapse]
Example 3

Input:

var aString = "this string has 29 characters"

Expected values:

reverse = "sretcarahc 92 sah gnirts siht"

Output:

"sretcarahc 92 sah gnirts siht"

[collapse]
Hint

Convert each character into a string and join them in reverse order.

[collapse]
Solution

var aString = "this string has 29 characters"
var reverse = ""

for character in aString.characters {
    var asString = "\(character)"
    reverse = asString + reverse
}

print(reverse)
//sretcarahc 92 sah gnirts siht

[collapse]

5.5 Palindrome

Print true if aString is a palindrome, and false otherwise. A palindrome is a string which reads the same backward or forward.

This content is restricted to buyers of Online Exercise Platform.

Example 1

Input:

var aString = "anutforajaroftuna"

Output:

true

[collapse]
Example 2

Input:

var aString = "Hello"

Output:

false

[collapse]
Example 3

Input:

var aString = "HelloolleH"

Output:

true

[collapse]
Hint

How can reversing a string help here ?

[collapse]
Solution

let aString = "anutforajaroftuna"

var reverse = ""

for character in aString.characters {
    var char = "\(character)"
    reverse = char + reverse
}

print(aString == reverse)

[collapse]

5.6 Words

This content is restricted to buyers of Online Exercise Platform.

Example 1

Input:

var problem ="split this string into words and print them on separate lines"

Output:

split
this
string
into
words
and
print
them
on
separate
lines

[collapse]
Hint

Iterate over the characters in the string. Keep track of the longest word you’ve encountered so far.

[collapse]
Solution

var problem = "split this string into words and print them on separate lines"

var word = ""

for character in problem.characters {
    if character == " " {
        print(word)
        word = ""
    } else {
        word += "\(character)"
    }
}

// don't forget the last word
print(word)

// split
// this
// string
// into
// words
// and
// print
// them
// on
// separate
// lines

[collapse]

5.7 Long word

This content is restricted to buyers of Online Exercise Platform.

Example 1

Input:

var problem = "find the longest word in the problem description"

Output:

description

[collapse]
Hint

Keep track of the longest word you encounter and also keep track of it’s length.

[collapse]
Solution

var problem = "find the longest word in the problem description"

// this will help the algorithm see the last word
problem += " "

var word = ""
var length = 0

var max = 0
var longestWord = ""

for character in problem.characters {
    if character == " " {
        if length > max {
            max = length
            longestWord = word
        }
        word = ""
        length = 0
    } else {
        word += "\(character)"
        length += 1
    }
}

print(longestWord)

[collapse]

5.8 Magic Time!

Use this magic * operator to solve the next challenge:

func *(string: String, scalar: Int) -> String {
    let array = Array(repeating: string, count: scalar)
    return array.joined(separator: "")
}

print("cat " * 3 + "dog " * 2)
// cat cat cat dog dog 

var newLine = "\n" * 2

print(newLine)
//
//

By using only one print() statement draw a rectangle of size N x M out of asterisks.

This content is restricted to buyers of Online Exercise Platform.

Example 1

Input:

var N = 5
var M = 10

Output:

**********
**********
**********
**********
**********

[collapse]
Example 2

Input:

var N = 2
var M = 2

Output:

**
**

[collapse]
Example 3

Input:

var N = 5
var M = 2

Output:

**
**
**
**
**

[collapse]
Hint

This problem can be solved by applying the magic operator exactly 2 times.

[collapse]
Solution

func *(string: String, scalar: Int) -> String {
    let array = Array(repeating: string, count: scalar)
    return array.joined(separator: "")
}

var newLine = "\n"

var N = 5
var M = 10

var line = "*" * M
line += newLine

var rectangle: String = line * N

print(rectangle)
// **********
// **********
// **********
// **********
// **********

[collapse]

 

Swift Programming from Scratch

Read more about the book here.

  1. First Steps
  2. Conditionals
  3. Types
  4. Loops
  5. Strings
  6. Arrays
  7. Functions
  8. Recursion
  9. Closures
  10. Tuples & Enums
  11. Dictionaries

?You can buy Training app  +  the PDF and ePub versions of the book!

    cover final small     +     cover final small

 

Feel free to ask any questions in the comments bellow. :)

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...

  28 comments for “Chapter 5: Strings

  1. HansHorstGlatt
    December 13, 2014 at 9:33 am

    Hi. Thanks a lot for the exercises. They are simple enough to be rapidly understood and therefore very useful. By the way, does playground generate some kind of “derived data” which should be deleted from time to time ?

    • December 13, 2014 at 11:15 am

      I have no idea :) – my hunch is no, since it recompiles and runs the code with every change.

  2. humberto
    February 6, 2015 at 4:26 pm

    Thank you so much for helping us out!

    What exactly is: +=
    I’m new to programming and trying to learn Swift as my first language
    Thank you!

  3. Irwin
    February 7, 2015 at 2:12 am

    I’ve been managing projects in software for over 20 years and have finally decided to learn how to code. Thank you very much for this fantastic site. It’s a game working through the exercises.

    Here’s a 1-line solution to your last problem.

    func *(string: String, scalar: Int) -> String {
    let array = Array(count: scalar, repeatedValue: string)
    return “”.join(array)
    }

    var w = 5
    var h = 10

    println(((” * ” * w) + “\n”) * h)

  4. Rajat pandey
    February 26, 2015 at 1:17 pm

    The Replace Question in string is not correct.
    Where does that “result” came from?

    • February 26, 2015 at 1:25 pm

      This is the full solution for that problem.

      var aString = “Replace the letter e with *”

      var replacedString = “”

      for scalar in aString.unicodeScalars {
      var char = “\(scalar)”
      if char == “e” {
      replacedString = result + “*”
      } else {
      replacedString = result + char
      }
      }

      • February 27, 2015 at 5:20 pm

        Actually you where right.
        var aString = “Replace the letter e with *”

        var replacedString = “”

        for scalar in aString.unicodeScalars {
        var char = “\(scalar)”
        if char == “e” {
        replacedString = replacedString + “*”
        } else {
        replacedString = replacedString + char
        }
        }

  5. an
    March 4, 2015 at 3:21 am

    problem 5.4 reverse

    i quite a bit confuse about the last line of the solution ( even your solution was right) is:

    reverse = asString + reverse

    for example 1:
    as the 1st loop running, the variable asString would be equal to ” H” while variable reverse still blank then when the last line executed , variable reverse will be fetched in value equal to “H”, then again and again.
    so i think the result will be ” Hello” as well.
    furthermore: 3 = 1 + 2 same as 3 = 2 +1.
    so :
    1st loop: reverse = “”+ “H” same as reverse = “H” +””
    there two equation still have same result right ?

    can you explain why my thinking went wrong pls ? explain the working mechanism of this solution ( hope that you dont mind)

    • SimNico
      March 15, 2015 at 11:34 pm

      “a” + “b” = “ab”
      but
      “b” + “a” = “ba”

  6. March 6, 2015 at 4:44 am

    Why do you need to use unicodedScaler? Everything works without it.

  7. March 6, 2015 at 4:56 am

    Alternate way to solve 5.7 Long Word

    var problem = “find the longest word in the problem description”
    var word = “”
    var longest = “”

    problem += ” ”

    for x in problem {
    if x != ” ” {
    word += “\(x)”
    } else {
    if countElements(word) > countElements(longest) {
    longest = word }
    word = “”
    }
    }

    println(longest)

  8. March 7, 2015 at 1:20 am

    Magic Time can be solved with 1 line of code

    func *(string: String, scalar: Int) -> String {
    let array = Array(count: scalar, repeatedValue: string)
    return “”.join(array)
    }

    var N = 5
    var M = 11

    var newLine = “\n”

    println((“*” * M + newLine) * N)

  9. SimNico
    March 15, 2015 at 11:46 pm

    I don’t understand why Swift let us use this as an infix operator :

    func *(string: String, scalar: Int) -> String {
    let array = Array(count: scalar, repeatedValue: string)
    return “”.join(array)
    }

    println(“cat ” * 3 + “dog ” * 2)
    // cat cat cat dog dog

    Is that because * is already an infix operator for Int, Double, Float, etc. and we’re just extending its accepted input types?

    Because while I can’t do that :

    func a(a:Int,b:Int) -> Int {
    return a + b
    }

    3 a 5
    // ERROR

    … this works fine :

    func *(a:Bool,b:Bool) -> Bool {
    return a && b
    }

    true * false
    // false

    func +(a:Bool,b:Bool) -> Bool {
    return a || b
    }

    true + false
    // true

    Please answer :(

    • March 16, 2015 at 1:09 pm

      Custom operators can begin with one of the ASCII characters /, =, -, +, !, *, %, < , >, &, |, , or ~, or any of the Unicode characters in the “Math Symbols” character set. – from Swift Operators

      • SimNico
        March 16, 2015 at 2:17 pm

        Ok but why don’t we have to first declare the operator beforehand ?

        infix operator * { /*…*/ }

        Is this because it’s already declared as it’s default for multiplication ?

        • March 16, 2015 at 2:50 pm

          I’m pretty sure thats the reason.

          • SimNico
            March 16, 2015 at 3:18 pm

            Thanks for your answers ! :)

          • March 16, 2015 at 3:33 pm

            :) anytime

  10. March 27, 2015 at 2:50 pm

    I have been making functions out of these problems so I can reuse them. Here is an example of the longest word exercise.

    func findLongest(testString:String) ->String {

    var wTestString = testString + ” ”
    var tmpHolder = “”
    var tCount = 0
    var theBiggest = “”

    for scalar in wTestString.unicodeScalars {

    var myCharAsString2 = “\(scalar)”

    if myCharAsString2 != ” “{

    tmpHolder += myCharAsString2

    } else {

    if countElements(tmpHolder) >= tCount{

    theBiggest = tmpHolder

    tCount = countElements(tmpHolder)

    }

    tmpHolder = “”

    }

    }
    return theBiggest
    }

    var myProblem = “find the longest word in the problem description”

    findLongest(myProblem)

    • March 27, 2015 at 6:49 pm

      That seems helpful :). A lot of the problems from the book are problems you face every day as a developer.

  11. April 8, 2015 at 3:48 pm

    For section 5.3 (Replace), you could also do this:

    let aString = “Replace the letter e with *”
    let replaced = String(map(aString.generate()) {
    $0 == “e” ? “*” : $0
    })

    println(“\(replaced)”)

  12. June 26, 2015 at 8:26 am

    Hey, UnicodeScalars is
    var someString = “this string has 29 characters”

    for scalar in someString.unicodeScalars {
    // to convert a UnicodeScalar into a String use string interpolation
    // you will need to do this in order to compare characters
    var characterAsString = “\(scalar)”

    println(characterAsString)
    }

    and we can even directly give in this way
    for i in astring{
    println(i)
    }

    Is it both are same or different. if so Explain it please

  13. J-Wag
    August 7, 2015 at 2:44 am

    I copied your example exactly for”countElements” gives me an error, but the function “count” does not. I am I using a different version of swift than you or something?

    • J-Wag
      August 7, 2015 at 2:48 am

      Nevermind, I am indeed using a newer version! And Swift 2 has further changed the syntax to something like “test1.characters.count” apparently.

  14. J-Wag
    August 7, 2015 at 3:18 am

    For 5.3, examples 1 and 2 are exactly the same! (I compared them with the “==” operator ;))

  15. Dennis
    July 22, 2016 at 2:15 pm

    For the reversed characters check I use the following method. The built in functions in Swift are actually very useful.

    let aString = “anutforajaroftuna”
    var reverse = String(aString.characters.reverse())

    if aString == reverse {
    print(“True”)
    } else {
    print(“False”)
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
We send about one email per week with our latest tutorials and updates
Never display this again :)