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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework