All TalkersCode Topics

Follow TalkersCode On Social Media

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

Reverse Each Word In A String In Java

Last Updated : Mar 11, 2024

Reverse Each Word In A String In Java

In this article we will show you the solution of reverse each word in a string in java, first we imported package and if you are using eclipse then all program will import itself during creation.

In main class we defined main static method and revWrds() methods.

The revWrds() method we need to use indexOf() method to separate the words in given string then we checking string whether valuable or not.

Then at last return the result to the function call and displayed result on terminal.

Step By Step Guide On Reverse Each Word In A String In Java :-

In main class ‘RevWRDS_prgm’, we defined main and revWrds() method definitions. In revWrds() method, we need to define if condition to checks passed string have enough value or not.

Then returning the result as per if condition result.

Here at last we returning reversed order words to the function call begin state and displaying final answer to the end user through printing on terminal.

//REVERSE EACH WORDS
package java_prgms;
class RevWRDS_prgm
{
 public static void main(String args[])
    {
        String strn="Hi How Are You";
        String res=revWrds(strn);
        System.out.println("Reversed Words : "+res);
    }
 public static String revWrds(String wrds){
  int i=wrds.indexOf(" ");
  if(i==-1)
   return wrds;
  return revWrds(wrds.substring(i+1))+" "+wrds.substring(0, i);
 }
}
  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 ‘RevWRDS_prgm’, which contains static main and revWrds() method definitions. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. In main method we declared string variable ‘strn’ and it is initialized with value ‘Hi How Are You’. Then you need to pass that variable into revWrds() method, which is result will be stored on variable ‘res’ and then printed result on terminal with string ‘Reversed Words :’ by println() method’s help.
  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 method revWrds() , passed argument denoted by variable ‘wrds’. When a method defined as public that not needs object help to execute the method code. So here you do not need to create object for main class.
  6. Within function, firstly we appending the passed value with indexOf() method to store whitespace count. If the whitespace count -1 then it’s single word you no need to process that word and send to main method.
  7. If not then you need to reverse them, so we considering words as sub array and connecting them by concatenation and substring() method then returned.
  8. 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 words in java. To compile java program you need to tell the java compiler which program to compile through ‘javac RevWRDS.java’.

After successful compilation you need to execute ‘java RevWRDS‘ line for gets the output.

Then you can see output ' Reversed Words : You Are How Hi'. On eclipse just press of run button then you will get output inside terminal.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪