0
0
Swiftprogramming~10 mins

Character and String types in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable named greeting with the value "Hello".

Swift
var greeting: String = [1]
Drag options to blanks, or click blank then click option'
A"hello"
B"Hello"
CHello
D'Hello'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings.
Not using quotes at all.
2fill in blank
medium

Complete the code to create a Character constant named letter with the value 'A'.

Swift
let letter: Character = [1]
Drag options to blanks, or click blank then click option'
AA
B'A'
C"A"
D"a"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for characters.
Not using quotes at all.
3fill in blank
hard

Fix the error in the code to concatenate two strings greeting and name with a space in between.

Swift
let fullGreeting = greeting [1] " " [2] name
Drag options to blanks, or click blank then click option'
A+
B&
C+=
D&&
Attempts:
3 left
💡 Hint
Common Mistakes
Using & or && which are not string concatenation operators in Swift.
Using += in the middle of an expression.
4fill in blank
hard

Fill both blanks to create a dictionary that maps each word to its length if the length is greater than 3.

Swift
let wordLengths = Dictionary(uniqueKeysWithValues: words.filter { word in word.[1] > 3 }.map { word in (word, word.[2]) })
Drag options to blanks, or click blank then click option'
Acount
Bword.length
Clen(word)
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) which is not valid in Swift.
Using word.length or word.size which do not exist in Swift.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps uppercase words to their lengths if length is greater than 4.

Swift
let result = Dictionary(uniqueKeysWithValues: words.filter { word in word.[3] > 4 }.map { word in (word.[1](), word.[2]) })
Drag options to blanks, or click blank then click option'
Auppercased
Bcount
Dlowercased
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercased() instead of uppercased().
Using incorrect length property.