Advertisement
KonstantyNil

Untitled

Sep 4th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.41 KB | None | 0 0
  1. func numberOfWords(in sentence: String) -> Int {
  2.     var count = 0
  3.     var inWord = false
  4.    
  5.     for character in sentence {
  6.         if character == " " {
  7.             if inWord {
  8.                 count += 1
  9.             }
  10.                 inWord = false
  11.             } else {
  12.                 inWord = true
  13.                 }
  14.             }
  15.     if inWord {
  16.         count += 1
  17.         }
  18.     return count
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement