Java String Manipulation: Reversing Word Order in a Sentence
Q - Write a Java program that reverses the order of words in a given sentence.
import java.awt.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.in;
public class demo {
public static void main(String[] args) {
String str = "My name is Gulshan";
String[] arr = str.split(" ");
int arrLen = arr.length;
List<String> result = new ArrayList<>();
for (int i=(arrLen-1); i>=0; i--){
result.add(arr[i]);
}
System.out.println("Original String: " + str);
System.out.print("String after reverse: " );
for (String a:result) {
System.out.print(a + " ");
}
}// write all the code above this line
}
// output
// Original String: My name is Gulshan
// String after reverse: Gulshan is name My
Subscribe to my newsletter
Read articles from Gulshan Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gulshan Kumar
Gulshan Kumar
As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman. I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.