w3resource

Swift Basic Programming Exercise: Add "Is" to the front of a given string with condition

Swift Basic Programming: Exercise-5 with Solution

Write a Swift program to add "Is" to the front of a given string. However, if the string already begins with "Is", return the given string.

Pictorial Presentation:

Swift Basic Programming Exercise: Add

Sample Solution:

Swift Code:

import Foundation 
func isstring(word: String) -> String {
    if word.hasPrefix("Is") == true
    {
        return word
    } 
    else 
    {
        return "Is \(word)"
    }
}

print(isstring(word: "Is Swift"))
print(isstring(word: "Swift"))

Sample Output:

Is Swift
Is Swift

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program to accept two integer values and return true if one is negative and one is positive.
Next: Write a Swift program to remove a character at specified index of a given non-empty string.

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