How to count the number of words in a string with Swift

1 Answer

0 votes
import Foundation

let s = "swift java c c++ python rust"

let strComponents = s.components(separatedBy: .whitespacesAndNewlines)

let wordsArray = strComponents.filter { !$0.isEmpty }

print(wordsArray.count) 


 
/*
run:
 
6

*/

 



answered Dec 7, 2024 by avibootz
...