HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Reverse words in string in Java

Reverse words in string in Java

Learn to reverse each word in a sentence in Java with example. We will reverse words in string in place using StringBuilder and StringUtils classes.

Original String – how to do in java

Reverse words – woh ot od ni avaj

1. Reverse words in string – StringBuilder class

  • Tokenize each word using String.split() method.
  • Loop through string array and use StringBuilder.reverse() method to reverse each word.
  • Join all revered words to get back string.
String blogName = "how to do in java";
StringBuilder reverseString = new StringBuilder();

String[] words = blogName.split(" ");		//step 1

for (String word : words) 
{
    String reverseWord = new StringBuilder(word).reverse().toString();		//step 2
    reverseString.append(reverseWord + " ");								//step 3
}

System.out.println( reverseString.toString().trim() );		//verify reversed string

Program output.

woh ot od ni avaj

2. Reverse words in string – StringUtils class

StringUtils class is in Apache command lang library. Use it’s StringUtils.reverseDelimited() method to reverse each word and join the string.

String blogName = "how to do in java";

String reverseString = StringUtils.reverseDelimited( StringUtils.reverse(sentence), ' ' );

System.out.println( reverseString );

Program output.

woh ot od ni avaj

In this post, we learned to reverse each word in a sentence in Java.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. ismail

    January 24, 2020

    How can I reverse 2 words in a string? For example reverse “Hi John” to “John Hi”.

    • Anton

      April 15, 2020

      // this is a little hard coded, you can’t use StringBuilder for this task
      String original = “Hi John”;
      String [] split = original.split(” “);
      String result = split[1] + ” ” + split[0];
      System.out.println(result);

  2. Jagannathan

    April 10, 2019

    @Rishab
    was this question is asked in amazon interview?

  3. Rishabh Gupta

    April 8, 2019

    How can i reverse word of string without using any inbuilt function?

    • Mohan Das

      November 7, 2019

      store it in character array .Overwrite accordance with reversal using INDEX OF CHARACTERS
      Here below is your program:-

      import java.util.Scanner;
      
      public class Reverse
      {
      	public static void main(String args[])
      	{
      		Scanner sc=new Scanner(System.in);
      
      		System.out.println("Enter the string:");
      
      		String s = sc.nextLine();
      
      		int l = s.length();
      
      		System.out.println("Length="+l);
      
      		char ch[] = s.toCharArray();
      
      		for(int i=l-1; i>=0; i--)
      		{
      			System.out.print(ch[i]);
      		}
      	}
      }
Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)