Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you rewrite this Java code in the Swift programming language? 1 public class

ID: 3878502 • Letter: C

Question

Can you rewrite this Java code in the Swift programming language?

1 public class StringDemo public static void main (String [] args) 4. 6 7 8 9 10 String sentence "Text processing is hard!"; int position sentence.indexOf ("hard"); System.out.println (sentence); System.out.println ("012345678901234567890123"); System.out.println ("The word I"hard" starts at index " + position); sentence sentence.substring (0, position) "easy!"; sentence sentence.toUpperCase (); System.out.println ("The changed string is:"); System.out println (sentence); 12 15 16

Explanation / Answer

import Foundation

var sentence = "Text processing is hard!"

//Finding the range of substring "hard"

let range: Range<String.Index> = sentence.range(of: "hard")!

//Finding the length of sentence excluding the length of hard

let position: Int = sentence.distance(from: sentence.startIndex, to: range.lowerBound)

//Finding the length of substring "hard"

var length = "hard".count

length = position + length   

print(sentence)

print("012345678901234567890123")

print("The word "hard" starts at index (position)")

let start = sentence.index(sentence.startIndex, offsetBy: position)

let end = sentence.index(sentence.startIndex, offsetBy: length)

sentence.replaceSubrange(start..<end, with: "easy")

sentence=sentence.uppercased()

print("The changes string is:")

print(sentence)

Above code is compiled on Swift Ver. 4.0.2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote