In this post, we will see “How to reverse characters of a word and also how to reverse words in a String in Java 8?”. To achieve that, we are going to use Stream API introduced in Java 8 and StringBuilder to reverse the string. 

See what we are looking to achieve here:

Reversing Character of a Word:

Original word: Welcome
Reversed word: emocleW


Reversing Word in a String:

Original String: Welcome to Techndeck.com
Reversed String: emocleW ot moc.kcednhceT 

Reverse Character & Word in a String in Java 8 with example...!!! Share on X

In this tutorial, we are going to cover below topics:

  1. How to reverse Character of a Word?
  2. How to reverse Word in a String?

Example 1. Java code to reverse character in a word

import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * To reverse word characters using Java 8
 * 
 * @author Deepak Verma
 *
 */
public class Reverse_Character_Word_String_Java8 {

    public static void main(String[] args) {

        Reverse_Character_Word_String_Java8 obj = new Reverse_Character_Word_String_Java8();

        obj.reverse_character_in_words("Welcome to Techndeck.com");

    }

    /**
     * To reverse character of a word using Java 8
     * 
     * @param str
     */
    public void reverse_character_in_words(String str) {
        String words[] = str.split(" ");

        Stream <String> streamOfWords = Arrays.stream(words);

        streamOfWords.forEach(w -> {

            System.out.println("Original word: " + w);

            Function <String,String> reverse = s -> new StringBuilder(s).reverse().toString();

            String reversedWord = reverse.apply(w);

            System.out.println("Reversed word: " + reversedWord + "\n");

        });

    }
}

Output:

Original word: Welcome
Reversed word: emocleW

Original word: to
Reversed word: ot

Original word: Techndeck.com
Reversed word: moc.kcednhceT

Example 2. Java code to reverse word in a string

import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * To reverse words in a string using Java 8
 * 
 * @author Deepak Verma
 *
 */
public class Reverse_Word_In_String_Java8 {

    public static void main(String[] args) {

        Reverse_Word_In_String_Java8 obj = new Reverse_Word_In_String_Java8();

        obj.reverse_words_in_String("Welcome to Techndeck.com");

    }

    /**
     * To reverse words in a String using Java 8
     * 
     * @param str
     */
    public void reverse_words_in_String(String str) {
        String words[] = str.split(" ");

        StringBuilder sb = new StringBuilder();

        Stream <String> stream = Arrays.stream(words);

        stream.forEach(w - > {

            Function <String,String> reverse = s -> new StringBuilder(s).reverse().toString();

            String reversedWord = reverse.apply(w);

            sb.append(reversedWord + " ");
        });

        System.out.println("Original String: " + str);
        System.out.println("Reversed String: " + sb);

    }


}

Output:

Original String: Welcome to Techndeck.com
Reversed String: emocleW ot moc.kcednhceT 

Reverse Character & Word in a String in Java 8 with example...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts