swift

ConvertToUpperCase()

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.

Strings
Functions
ASCII Characters
Easy dificulty

Creating a Swift Function to Convert Text to Uppercase

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.

Step 1: Create a Swift function

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) {

}

Step 2: Add functionality to convert characters

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()
}

Step 3: Returning the Result

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
}

Step 4: Function Usage

Now we can call our function and pass it a string, the function will return the uppercased string.

let result = convertToUpperCase(input: "hello")

Conclusion

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.

Learn function in:

String Case Conversion

Conversion of string characters to upper case

Learn more

Mathematical principle

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