How do I return string made of every other character from giving string? Like “house” to “hue” help me I wanna do that.
Asked
Active
Viewed 222 times
-2
-
8You surely tried *something.* Don't hesitate to show your attempt, so that it does not look like a “write the code for me” question! – Martin R Aug 24 '20 at 15:22
-
https://stackoverflow.com/a/41030422/2303865 – Leo Dabus Aug 24 '20 at 20:16
1 Answers
-2
one not very fancy but working way would be:
print(stringFromEveryOtherCharacter("house"))
func stringFromEveryOtherCharacter(_ string: String) -> String {
var result : String = ""
for (index, char) in string.enumerated() {
if index % 2 == 0 {
result = result + String(char)
}
}
return result
}
which returns "hue"

mimo
- 741
- 1
- 6
- 13
-
-
This is not a complete program. This is just a simple draft. It should run in a Playground. – mimo Aug 24 '20 at 16:18
-
If you vote down you should offer an alternative (there are numerous) solution. – mimo Aug 24 '20 at 16:19
-