Q. Write a program to reverse the words of the string object.
Answer:
It is a program to reverse the word of the given sentence. The split () method is used to split the string when it matches of the given regular expression.
Output:

Answer:
It is a program to reverse the word of the given sentence. The split () method is used to split the string when it matches of the given regular expression.
ReverseWordofString.java
import java.util.Scanner;
class ReverseWordofString
{
public static String reverseString(String s)
{
String[] str1 = s.split(" ");
String result1 = "";
for (int i = str1.length-1; i >= 0; i--)
{
result1 += str1[i]+" ";
}
return result1;
}
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter the String: ");
String str = scn.nextLine();
String result = ReverseWordofString.reverseString(str);
System.out.println("\nGiven String: "+str);
System.out.println("\nReverse String: "+result);
}
}
Output:



