Reverse Strings with Varargs in Java


Write a Java program that reverses each string in a set of strings using varargs

This Java program reverses a list of strings. It defines a method called reverseStrings that takes multiple strings as input, reverses each string, and returns an array containing the reversed strings. In the main method, it calls this function with some sample strings and prints out the reversed strings.

Source Code

import java.util.Arrays;
public class StringReverser
{
	static String[] reverseStrings(String... strings)
	{
		System.out.println("Given Strings : " + Arrays.toString(strings));
		String[] reversed = new String[strings.length];
		for (int i = 0; i < strings.length; i++)
		{
			reversed[i] = new StringBuilder(strings[i]).reverse().toString();
		}
		return reversed;
	}
 
	public static void main(String[] args)
	{
		String[] reversedStrings = reverseStrings("Blue", "Pink", "Black", "White");
		System.out.println("Reversed Strings : " + Arrays.toString(reversedStrings));
	}
}

Output

Given Strings : [Blue, Pink, Black, White]
Reversed Strings : [eulB, kniP, kcalB, etihW]

Example Programs