w3resource

Swift Programming Exercises, Practice, Solution: String

Swift String Programming [21 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a Swift program to draw a HTML string as bold or italic text.
Click me to see the solution

2. Write a Swift program to insert a given string to another given string where the second string will be in the middle of the first string.
Click me to see the solution

3. Write a Swift program to create a string made of two copies of the last two characters of a given string. The given string length must be at least 2.
Click me to see the solution

4. Write a Swift program to create a new string made of a copy of the first two characters of a given string. If the given string is shorter than length 2, return whatever there is.
Click me to see the solution

5. Write a Swift program to return the first half of a given string of even length.
Click me to see the solution

6. 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.
Click me to see the solution

7. 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.
Click me to see the solution

8. Write a Swift program that accept two strings and return their concatenation, except the first char of each string. The given strings length must be at least 1.
Click me to see the solution

9. Write a Swift program to move the first two characters of a given string to the end. The given string length must be at least 2.
Click me to see the solution

10. Write a Swift program to move the last two characters of a given string to the start. The given string length must be at least 2.
Click me to see the solution

11. Write a Swift program to create a new string without the first and last character of a given string. The string may be any length, including 0.
Click me to see the solution

12. Write a Swift program to create a new string taking the middle two characters of a given string of even length. The given string length must be at least 2.
Click me to see the solution

13. Write a Swift program to test if a given string starts with "ab".
Click me to see the solution

14. Write a Swift program to create a new string made of the first and last n chars from a given string. The string length will be at least n.
Click me to see the solution

15. Write a Swift program to create a new string of length three from a given string of odd length from its middle. The string length must be three.
Click me to see the solution

16. Write a Swift program to concate two given strings and return the new string. If the new string creates a double character then omit one of the character. so "vwx" and "xyz" will return "vwxyz".
Click me to see the solution

17. Write a Swift program to create a new string of any length from a given string where the last two characters are swapped, so "abcde" will be "abced".
Click me to see the solution

18. Write a Swift program to return "abc" or "xyz" if a given string begins with "abc" or "xyz" otherwise return the empty string.
Click me to see the solution

19. Write a Swift program to check if the first two characters are same of the last two characters of a given string.
Click me to see the solution

20. Write a Swift program to create a new string made of 2 copies of the first 2 characters of a given string. The string may be any length.
Click me to see the solution

21. Write a Swift program to check if the first or last characters are 'a' of a given string, return the given string without those 'a' characters, and otherwise return the given string.
Click me to see the solution

Swift Programming Code Editor:



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://w3resource.com/swift-programming-exercises/string/index.php