Swift String Exercises: Create a new string without the first and last character of a given string
Write a Swift program to create a new string without the first and last character of a given string. The string length must be at least 2.
Pictorial Presentation:
![Flowchart: Swift String Exercises - Create a new string without the first and last character of a given string.](https://www.w3resource.com/w3r_images/swift-string-image-exercise-6.png)
Sample Solution:
Swift Code:
func without_first_last(_ input: String) -> String {
var new_str = input.characters
new_str.removeFirst()
new_str.removeLast()
return String(new_str)
}
print(without_first_last("Hello"))
print(without_first_last("Swift"))
Sample Output:
ell wif
Swift Programming Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Swift program to return the first half of a given string of even length.
Next: Write a Swift program that accept two strings of different length and return a string of the form short+long+short. Two given string's length may be 0
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics