Uppercase Concatenated Strings with Varargs in Java
Write a Java program that concatenates strings using varargs and then converts the result to uppercase
- The class contains a method called concatenateAndConvertToUppercase, which takes a variable number of strings as input. Inside this method, it iterates through each string in the input array and appends them together using a StringBuilder.
- After appending all the strings, it converts the concatenated string to uppercase using the toUpperCase() method.
- The main method is the entry point of the program. Inside main, it calls the concatenateAndConvertToUppercase method with the strings "Tutor " and "joes" as arguments. It stores the concatenated and uppercase result in the variable concatenatedUppercase.
- Finally, it prints out the result using System.out.println, displaying "Concatenated and Uppercase : " followed by the concatenated string converted to uppercase.
Source Code
public class UppercaseConcatenator
{
static String concatenateAndConvertToUppercase(String... strings)
{
StringBuilder result = new StringBuilder();
for (String str : strings)
{
result.append(str);
}
return result.toString().toUpperCase();
}
public static void main(String[] args)
{
String concatenatedUppercase = concatenateAndConvertToUppercase("Tutor ", "joes");
System.out.println("Concatenated and Uppercase : " + concatenatedUppercase);
}
}
Output
1 is Prime ? false
2 is Prime ? false
3 is Prime ? false
4 is Prime ? false
5 is Prime ? false