Swift Basic Programming Exercise: Create a string taking characters at indexes 0, 2, 4, 6, 8, .. from a given string
Swift Basic Programming: Exercise-26 with Solution
Write a Swift program to create a string taking characters at indexes 0, 2, 4, 6, 8, .. from a given string.
Pictorial Presentation:
Sample Solution:
Swift Code:
func chars_string(_ input: String) -> String {
var new_str = input
var count = 1
while count < (new_str.characters.count) {
new_str.remove(at: new_str.index(new_str.startIndex, offsetBy: count))
count += 1
}
return new_str
}
print(chars_string("abcdefgh"))
print(chars_string("abcdefg"))
print(chars_string("abcdef"))
Sample Output:
aceg aceg ace
Swift Programming Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Swift program to create a new string where all the character "a" have been removed except the first and last positions.
Next: Write a Swift program to count the number of times that two 7's are next to each other in a given array of integers.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/swift-programming-exercises/basic/swift-basic-exercise-26.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics