w3resource

Swift Basic Programming Exercise: Test a given string whether it starts with "Is"

Swift Basic Programming: Exercise-11 with Solution

Write a Swift program to test a given string whether it starts with "Is". Return true or false.

Pictorial Presentation:

Swift Basic Programming Exercise: Test a given string whether it starts with 'Is'.

Sample Solution:

Swift Code:

func start_is(str1: String) -> Bool {
    let str2 = str1.characters
    let hello = "Is"
    let first2Values = str2.prefix(2)
    let first_two = String(first2Values)
    
    if hello == first_two {
        return true
    } else {
        return false
    }
}


print(start_is(str1: "Is Swift"))
print(start_is(str1: "is python"))
print(start_is(str1: "java is"))

Sample Output:

true
false
false

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program to take the first two characters from a given string and create a new string with the two characters added at both the front and back.
Next: Write a Swift program that return true if either of two given integers is in the range 10..30 inclusive.

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-11.php