All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Reverse A Sentence In Java

Last Updated : Mar 11, 2024

How To Reverse A Sentence In Java

In this article we will show you the solution of how to reverse a sentence in java, as usual imported package and we need pattern object help so we imported regex.pattern packages as well. In class we defined main static method and revSEN() methods.

The revSEN() method processing passed default sentence at main method.

Here using pattern object we separating space between words and stored on array.

Then iterating words through for loop and reversely attaching words each other and return to the main method.

Step By Step Guide On How To Reverse A Sentence In Java :-

Defined main class ‘RevSen_prgm’, there we provide main and revSEN() method definitions. In revSEN() function, you need to create pattern object which is named as ‘ptr’.

It can finds out spaces in a sentence, so binding that with sentence variable and split method.

Then only you get separated words in given sentence and stored on array variable ‘t’. In for loop we appending each words reversely and return to main method.

In main method we displaying reversed sentence at terminal and end user display.

//REVERSE SENTENCE
package java_prgms;
import java.util.regex.Pattern;
class RevSen_prgm
{
 static String revSEN(String s){
  Pattern ptr=Pattern.compile("\\s");
  String[] t=ptr.split(s);
  String res=" ";
  for(int i=0;i<t.length;i++){
   if(i==t.length-1)
    res=t[i]+res;
   else
    res=" "+t[i]+res;
  }
  return res;
 }
 public static void main(String args[])
    {
        String stn="This is reverse sentence example in java language";
        System.out.println(revSEN(stn));
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. Here we defined class ‘RevSen_prgm’, which contains static main method and revSEN(). The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. There we defined sentence ‘This is reverse sentence example in java language’ and stored on string variable ‘stn’. Then using println() method we calling revSEN() function with sentence.
  4. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  5. In revSEN() function, firstly you need to create pattern object that is ‘ptr’, which is used to collect white space between words.
  6. Here we using that with split method, which is holding sentence ‘s’ passed value. It’s result given sentence separated by spaces and stored on string data type array variable ‘t’.
  7. The empty string variable ‘res’ defined for appends the result one by one in reverse order. Then we defined for loop, which is iterates the two line of code until reaches words length.
  8. Within loop we need to check whether length of the sentence one or below then we sends the sentence as it is to the main method to print on terminal.
  9. Else we appending each words one by one on variable ‘res’ in the reverse order with the help of concatenation ‘+’ and at last we sends result to main method.
  10. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to reverse given sentence in java. To compile java program you need to tell the java compiler which program to compile through ‘javac RevSen.java’.

After successful compilation you need to execute ‘java RevSen‘ line for gets the output. Then you can see output 'language in java example sentence reverse is This'.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on how to reverse a sentence in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪