How to capitalize 2 nth letter in a sentence

how do I capitalize any letters of user input:
example;

sentence.text : my name is
intA.text : 2
intB.text: 5

answer would be: mY nAme is

Is this homework?

What have you tried so far?

2 Likes
func capitalize(string: String, positions: IndexSet) -> String {
    let range = (0..<string.count)
    guard string.count > 0,
        positions.allSatisfy({ range ~= $0 }) else {
        fatalError("Couldn't capitalize")
    }

    return String(string.enumerated().map{ (index, char) in
        return positions.contains(index) ? String(char).capitalized.first! : char
    })
}

but its not working

extension String {
  func uppercaseCharAt(_ idx : Int...) -> String {
    return self.enumerated().map({ idx.contains($0) ? String($1).uppercased() : String($1) }).joined()
  }
}

let st = "a quick movement".uppercaseCharAt(5, 9, 11);
1 Like
Terms of Service

Privacy Policy

Cookie Policy