swift
Parameters: sourceString: String
String to be converted to uppercase
Returns: Returns the string converted to upper case
A Swift function that converts an input string of lowercase letters into uppercase. It uses Swift's built-in 'uppercased()' method.
Welcome, dear programmer. We are pleased to have you on board. Without much ado, we will walk you through the steps to create a 'convert-to-uppercase' function in Swift. This will empower you to manipulate strings with great ease across your projects. Enjoy the journey of exploring the elegance and simplicity of Swift.
First of all, you'll need to define a function in Swift. We're going to call it convertToUpperCase
that will take a string as an input.
func convertToUpperCase(input: String) {
}
Inside the brackets, add a line of code to transform the string to all uppercase letters. Swift has a built-in method called uppercased()
which converts all the characters in a string to uppercase.
func convertToUpperCase(input: String) {
let uppercasedString = input.uppercased()
}
We need the function to return the converted string. So, let's modify our function definition to specify that it returns a string, and add a return
statement.
func convertToUpperCase(input: String) -> String {
let uppercasedString = input.uppercased()
return uppercasedString
}
Now we can call our function and pass it a string, the function will return the uppercased string.
let result = convertToUpperCase(input: "hello")
This is the final implementation of the function. This function takes a string, converts it to uppercase and returns the uppercased string.
func convertToUpperCase(input: String) -> String {
let uppercasedString = input.uppercased()
return uppercasedString
}
In Swift, transforming a string to uppercase is quite straightforward, and all thanks to its built-in uppercased()
method.
To use the function, you simply call convertToUpperCase(input: stringToConvert)
, assign its return value to a variable, and then you can use that variable in your code.
This function uses the ASCII value of characters. In the ASCII table, each uppercase character has a different value compared to its lowercase equivalent. In Swift, the 'uppercased()' method automatically converts the ASCII value of a lowercase letter to its corresponding uppercase character.
Learn more