How To Print All The Word In Reverse Order In Array Of String In Java.

Print all the word in reverse order in array of string in java

First of all we will see the little demo of use of substring mathod in java because in this java program session we will use the substring method.

If you are knows what is substring and use of substring then you can go directly went to main java program soruce code by clicking below second link.

1. Introducation to substring() method in java.

2. How to print all the word in reverse order in array of string (source code).

Introducation and use of substring() method :

The substring() method extracts characters, between two indices (index), from a string, and return a new string that is a substring of this string. The substring() method takes two parameters first is Start Index and End Index.The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1 if the second argument is given. The substring() method does not change the original string.

Example - 1 :

public class substringDemo {
    public static void main(String args[])
    {
        String Str = new String("Welcome to java industry");
        
        System.out.print("The extracted substring is : ");
        System.out.println(Str.substring(10));
    }
}

Output:

java industry

Example -2 :

public class substringDemo {
    public static void main(String args[])
    {
        String Str = new String("Welcome to java industry");

        System.out.print("The extracted substring  is : ");
        System.out.println(Str.substring(10, 16));
    }
}

Output:

java

Source code for how to print array string in reverse order :

package gsh.java.programs;
public class ReverseArrayStringDemo {
    public static void main(String[] args) {
    
        String[] strArray = {"January","February","March","April","May","Jun","July","August"};
        String[] disStrArray = new String[strArray.length];
        for (int i=0;i<strArray.length;i++){
            disStrArray[i]="";  //define empty string instead of null
            for(int j=strArray[i].length()-1;j>=0;j--){
                disStrArray[i] += strArray[i].substring(j,j+1);
            }
        }
        for (int i=0;i< strArray.length;i++){
            System.out.print(disStrArray[i]+" ");
        }
    }
}

Output:

yraunaJ yraurbeF hcraM lirpA yaM nuJ yluJ tsuguA

Post a Comment

0 Comments