Create a new string made of 2 copies of the first 2 characters of a given string
import Foundation
func two_copies(_ str1: String) -> String {
if str1.characters.count == 0 {
return ""
}
else if str1.characters.count == 1
{
return str1 + str1
}
else
{
var currentIndex = str1.index(after: str1.startIndex)
currentIndex = str1.index(after: currentIndex)
let res = str1.substring(to: currentIndex)
return res + res
}
}
print(two_copies("Swift"))
print(two_copies("Online"))
print(two_copies("JS"))
Output:
SwSw
OnOn
JSJS
