74. The algorithm is simple and swift number of words in a string

Count the number of words in the string, the word here refers to the continuous character is not a space.

Note that you can assume that the string was does not include any non-printable characters.

Example:

Enter: "Hello, my name is John "
Output: 5

solution:

    func countSegments(_ s: String) -> Int {
                guard !s.isEmpty else {
            return 0
        }
        
        var s = s
        var  array = s.split(separator: " ")
        return array.count

    }

 

Guess you like

Origin blog.csdn.net/huanglinxiao/article/details/93160419