Reverse words in a given string.

Gulshan KumarGulshan Kumar
1 min read

Write a Java program that takes a string S as input and reverses the order of the words while keeping the individual words unchanged. The input string will be a series of words separated by dots (.).

Question - GeeksForGeeks

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 S = "i.like.this.program.very.much";

        String[] arr = S.split("\\.");
        int arrLen = arr.length;
        int x = arrLen-1;

        List<String> result = new ArrayList<>();
        for (int i=x; i>=0; i--){
            if(x>=i && i!=0) {
                result.add(arr[i] + ".");
            }
            else{
                result.add(arr[i]);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String str : result) {
            sb.append(str);
        }
        String reverseString = sb.toString();
        System.out.println(reverseString);



    }// write all the code above this line
}
0
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.