Practice 50 Swift String Programming Questions
Q1. Write a Swift program to find the length of the string "Hello, Swift!".
Input: "Hello, Swift!"
Expected Output: 13
Q2. Write a Swift program to convert the string "swift" to uppercase.
Input: "swift"
Expected Output: "SWIFT"
Q3. Write a Swift program to convert the string "SWIFT" to lowercase.
Input: "SWIFT"
Expected Output: "swift"
Q4. Write a Swift program to check if the string "apple" starts with the prefix "ap".
Input: "apple", "ap"
Expected Output: true
Q5. Write a Swift program to check if the string "banana" ends with the suffix "na".
Input: "banana", "na"
Expected Output: true
Q6. Write a Swift program to concatenate two strings "Hello" and "World".
Input: "Hello", "World"
Expected Output: "HelloWorld"
Q7. Write a Swift program to replace all occurrences of "e" with "a" in the string "elephant".
Input: "elephant", "e", "a"
Expected Output: "alaphant"
Q8. Write a Swift program to check if the string "swift" contains the substring "if".
Input: "swift", "if"
Expected Output: true
Q9. Write a Swift program to split the string "apple,banana,grape" by commas.
Input: "apple,banana,grape"
Expected Output: ["apple", "banana", "grape"]
Q10. Write a Swift program to reverse the string "hello".
Input: "hello"
Expected Output: "olleh"
Q11. Write a Swift program to check if the string "radar" is a palindrome.
Input: "radar"
Expected Output: true
Q12. Write a Swift program to count the number of vowels in the string "education".
Input: "education"
Expected Output: 5
Q13. Write a Swift program to count the occurrences of the character "a" in the string "banana".
Input: "banana", "a"
Expected Output: 3
Q14. Write a Swift program to trim whitespaces from the string " swift ".
Input: " swift "
Expected Output: "swift"
Q15. Write a Swift program to repeat the string "abc" 3 times.
Input: "abc", 3
Expected Output: "abcabcabc"
Q16. Write a Swift program to find the index of the first occurrence of "o" in the string "hello".
Input: "hello", "o"
Expected Output: 4
Q17. Write a Swift program to find the index of the last occurrence of "n" in the string "banana".
Input: "banana", "n"
Expected Output: 4
Q18. Write a Swift program to extract the substring "swift" from "learn swift programming".
Input: "learn swift programming", 6, 11
Expected Output: "swift"
Q19. Write a Swift program to check if two strings "abc" and "ABC" are equal (case-insensitive).
Input: "abc", "ABC"
Expected Output: true
Q20. Write a Swift program to check if the string "swift" is empty.
Input: ""
Expected Output: true
Q21. Write a Swift program to convert the first letter of "swift" to uppercase.
Input: "swift"
Expected Output: "Swift"
Q22. Write a Swift program to find the ASCII value of the first character of "A".
Input: "A"
Expected Output: 65
Q23. Write a Swift program to join the strings "red", "blue", and "green" with a separator "-".
Input: ["red", "blue", "green"], "-"
Expected Output: "red-blue-green"
Q24. Write a Swift program to replace the first occurrence of "cat" with "dog" in "catapult".
Input: "catapult", "cat", "dog"
Expected Output: "dogapult"
Q25. Write a Swift program to find the character at index 3 of the string "swift".
Input: "swift", 3
Expected Output: "i"
Q26. Write a Swift program to count the number of words in the string "hello world swift".
Input: "hello world swift"
Expected Output: 3
Q27. Write a Swift program to check if the string "abcd" is alphanumeric.
Input: "abcd1234"
Expected Output: true
Q28. Write a Swift program to format the string "Hello, {name}" by replacing {name} with "John".
Input: "Hello, {name}", "John"
Expected Output: "Hello, John"
Q29. Write a Swift program to find the Unicode scalar of the character "😄".
Input: "😄"
Expected Output: 128516
Q30. Write a Swift program to append "world" to "hello".
Input: "hello", "world"
Expected Output: "helloworld"
Q31. Write a Swift program to find the number of uppercase characters in "Swift Programming".
Input: "Swift Programming"
Expected Output: 2
Q32. Write a Swift program to find the number of lowercase characters in "Swift Programming".
Input: "Swift Programming"
Expected Output: 15
Q33. Write a Swift program to check if "hello world" contains only alphabetic characters.
Input: "hello world"
Expected Output: false
Q34. Write a Swift program to find the longest word in the string "Swift is awesome".
Input: "Swift is awesome"
Expected Output: "awesome"
Q35. Write a Swift program to replace all spaces in "hello world" with underscores.
Input: "hello world"
Expected Output: "hello_world"
Q36. Write a Swift program to check if the string "swift" contains the character "z".
Input: "swift", "z"
Expected Output: false
Q37. Write a Swift program to extract the first word from "Swift programming is fun".
Input: "Swift programming is fun"
Expected Output: "Swift"
Q38. Write a Swift program to check if the string "swift" has at least one uppercase letter.
Input: "swift"
Expected Output: false
Q39. Write a Swift program to find the shortest word in the string "I love Swift".
Input: "I love Swift"
Expected Output: "I"
Q40. Write a Swift program to remove all numeric digits from the string "swift123".
Input: "swift123"
Expected Output: "swift"
Q41. Write a Swift program to remove all vowels from the string "education".
Input: "education"
Expected Output: "dctn"
Q42. Write a Swift program to count the number of consonants in the string "programming".
Input: "programming"
Expected Output: 8
Q43. Write a Swift program to insert "abc" at index 2 of the string "12345".
Input: "12345", "abc", 2
Expected Output: "12abc345"
Q44. Write a Swift program to extract all numeric digits from the string "a1b2c3".
Input: "a1b2c3"
Expected Output: "123"
Q45. Write a Swift program to remove the first 3 characters from the string "swift".
Input: "swift", 3
Expected Output: "ft"
Q46. Write a Swift program to count the number of spaces in the string "hello world swift".
Input: "hello world swift"
Expected Output: 2
Q47. Write a Swift program to check if "12345" is a numeric string.
Input: "12345"
Expected Output: true
Q48. Write a Swift program to capitalize the first letter of every word in the string "hello swift world".
Input: "hello swift world"
Expected Output: "Hello Swift World"
Q49. Write a Swift program to remove the last character from the string "Swift".
Input: "Swift"
Expected Output: "Swif"
Q50. Write a Swift program to find the number of times "is" appears in the string "This is Swift programming.".
Input: "This is Swift programming."
Expected Output: 2
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!