w3resource

Swift Basic Programming Exercise: Check if a given string begins with "fix", except the 'f' can be any character or number

Swift Basic Programming: Exercise-13 with Solution

Write a Swift program to check if a given string begins with "fix", except the 'f' can be any character or number.

Pictorial Presentation:

Swift Basic Programming Exercise: Check if a given string begins with

Sample Solution:

Swift Code:

import Foundation

func fix_Start(_ input: String) -> Bool {
    var newInput = input
    let startIndex = newInput.startIndex
    let first_char = newInput.remove(at: startIndex)
    
    if newInput.hasPrefix("ix") == true 
    {
        newInput.characters.removeFirst(3)
        newInput.insert(first_char, at: startIndex)
        return true
    } 
    else
    {
        return false
    }
}


print(fix_Start("fix gold"))
print(fix_Start("six gold"))
print(fix_Start("1ix gold"))
print(fix_Start("gold"))
print(fix_Start("fax gold"))

Sample Output:

true
true
true
false
false

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program that return true if either of two given integers is in the range 10..30 inclusive.
Next: Write a Swift program to find the largest number among three given integers.

What is the difficulty level of this exercise?



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://www.w3resource.com/swift-programming-exercises/basic/swift-basic-exercise-13.php