get the first letter of each word in a string using objective-c

Viewed 20159

Example of what I am trying to do:

String = "This is my sentence"

I am looking to get this as a result: "TIMS"

I am struggling with objective-c and strings for some reason

3 Answers

The shortest and faster way to enumerate through the string using below code

Swift

let fullWord = "This is my sentence"
var result = ""
fullWord.enumerateSubstrings(in: fullWord.startIndex..<fullWord.endIndex, options: .byWords) { (substring, _, _, _) in
if let substring = substring { 
    result += substring.prefix(1).capitalized }
}
print(result)

Output

TIMS

Related