Skip to content

Instantly share code, notes, and snippets.

@AustinBCole
Created December 12, 2018 16:46
Show Gist options
  • Save AustinBCole/cc1b9258c532922f1c7f4a4b2b584714 to your computer and use it in GitHub Desktop.
Save AustinBCole/cc1b9258c532922f1c7f4a4b2b584714 to your computer and use it in GitHub Desktop.
Assumptions:
I will need to guard against an empty string. I will need to ignore anything that is not a letter.
Test Cases:
1. AbCdE
2. Fat Cat
3. "Ft6lKI9 !m"
Approach:
I know that there is a method in Javascript that turns a string into an array of its characters. I will use documentation to
look for a similar method. If I can do that, then I will simply loop through each index of the array and so long as the index
contains a letter I will use a ternary statement to see if it is lowercased. If it is then I will change it to uppercase, if
it isn't then I will change it to lowercase. I will then join the array together back into a string and return the string.
Code:
func reversedCase(string: String) -> String {
var newArray : [String] = []
guard string != "" else {return ""}
for character in string {
let lowercasedCharacter = String(character).lowercased()
String(character) == lowercasedCharacter ? newArray.append(String(character).uppercased()) : newArray.append(String(character).lowercased())
}
return newArray.joined()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment