Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
540 B
2
Indexable
Never
let inputString = "Swift is fun and powerful"
var reversedWordsString = ""
var currentWord = ""

for char in inputString {
    if char == " " {
        let reversedCurrentWord = String(currentWord.reversed())
        reversedWordsString += reversedCurrentWord + " "
        currentWord = ""
    } else {
        currentWord.append(char)
    }
}

// Handle the last word (no space after the last word)
let reversedLastWord = String(currentWord.reversed())
reversedWordsString += reversedLastWord

print(reversedWordsString)
Leave a Comment