Green - example 2

Open ViewController.swift, and replace the implementation of makeHeadline(from:) with the following lines of code:

func makeHeadline(from string: String) -> String { 
  let words = string.components(separatedBy: " ") 
   

  var headline = "" 
  for var word in words { 
    let firstCharacter = word.remove(at: word.startIndex) 
    headline += "\(String(firstCharacter).uppercased())\(word) " 
  } 
   
  headline.remove(at: headline.index(before: headline.endIndex)) 
  return headline 
} 

Let's go through this implementation step by step:

  1. Split the string into words.
  2. Iterate over the words, and remove the first character and change it to uppercase. Add the changed character to the beginning of the word. Add this word with a trailing space to the headline string. ...

Get Test-Driven iOS Development with Swift 4 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.