java
Parameters: String input
A string which will be converted to uppercase
Returns: The input string converted to uppercase
The convertToUpperCase function takes a string as input and returns a new string where all the characters are converted to upper case.
Welcome, aspiring programmer. We're about to embark on a journey into the world of Java. The steps below will guide you through programming a function to convert text to uppercase. As you follow along, remember: each line of code brings you closer to mastering the art of programming. Stay curious, stay determined, and enjoy the ride!
The first step is to understand what we need to accomplish. Here, we want to write a Java function that transforms a string to uppercase. This implies transforming all lower-case letters in the string to upper-case.
In Java, strings are objects that have various methods helpful in programming. The one that we are interested in is the toUpperCase()
method. It converts all the characters in the string from lower-case to upper-case.
In Java, you'd do that as follows:
String str = "This is a sample string";
str = str.toUpperCase();
After execution str would contain "THIS IS A SAMPLE STRING".
Let's create an toUpperCase()
function which takes a string input and returns an uppercase string.
Here's the code:
public String toUpperCase(String str) {
return str.toUpperCase();
}
It's always good practice to test your function to make sure it's working as expected. Here's how you could do it:
public static void main(String[] args) {
System.out.println(toUpperCase("This is a sample string")); // THIS IS A SAMPLE STRING
}
After running the main method, you should see the uppercase string printed in your console.
In this exercise, we've created a simple function to convert a given string to uppercase using Java language built-in methods. We've also learnt how to call and test this function.
Here's the full code:
public class Main {
public static void main(String[] args) {
System.out.println(toUpperCase("This is a sample string")); // THIS IS A SAMPLE STRING
}
public static String toUpperCase(String str) {
return str.toUpperCase();
}
}
This function doesn't rely on a specific mathematical principle but rather on a simple programming operation that consists of changing a string into its uppercase form. In `Java`, this operation is performed using the `toUpperCase()` method on the `String` object.
Learn more