Reading through this problem in a book
Given a string that contains both letters and numbers, write a function that pulls out all the numbers then returns their sum. Sample input and output
The string “a1b2c3” should return 6 (1 + 2 + 3). The string “a10b20c30” should return 60 (10 + 20 + 30). The string “h8ers” should return “8”.
My solution so far is
import Foundation
func sumOfNumbers(in string: String) -> Int {
var numbers = string.filter { $0.isNumber }
var numbersArray = [Int]()
for number in numbers {
numbersArray.append(Int(number)!)
}
return numbersArray.reduce(0, { $0 * $1 })
}
However, I get the error
Solution.swift:8:33: error: cannot convert value of type 'String.Element' (aka 'Character') to expected argument type 'String'
numbersArray.append(Int(number)!)
^
And I'm struggling to get this number
of type String.Element
into a Character
. Any guidance would be appreciated.