w3resource

Java: Reverse the content of a sentence without reverse every word


Reverse Sentence Without Words

Write a Java program to reverse a sentence (assume a single space between two words) without reverse every word.

Visual Presentation:

Java Basic Exercises: Reverse the content of a sentence without reverse every word.


Sample Solution:

Java Code:

// Importing the required Java utilities package
import java.util.*;

// Defining a class named Solution
public class Solution {
  
  // Method to reverse the words in a given string
  public static String reverse_str_word(String input_sentence) {
    // Checking if the input string is null
    if (input_sentence == null) {
      throw new IllegalArgumentException("Input param can't be null."); // Throwing an exception for null input
    }
    
    StringBuilder stringBuilder = new StringBuilder(); // Creating a StringBuilder to store the reversed string
    String[] words = input_sentence.split(" "); // Splitting the input sentence into words based on spaces
    
    // Loop to append words in reverse order to the StringBuilder
    for (int i = words.length - 1; i >= 0; i--) {
      stringBuilder.append(words[i]); // Appending each word in reverse order
      
      if (i != 0) {
        stringBuilder.append(" "); // Adding space between words except for the last word
      }
    }
    
    return stringBuilder.toString(); // Returning the reversed string of words
  }
  
  // The main method of the program
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in); // Creating a Scanner object to read input from the user
    
    // Asking the user to input a string
    System.out.print("Input a string: ");
    String input = scanner.nextLine(); // Reading the input string from the user
    
    // Displaying the result by reversing the words in the input string
    System.out.println("\nResult: " + reverse_str_word(input));
  }
} 

Sample Output:

Input a string:  The quick brown fox jumps over the lazy dog

Result: dog lazy the over jumps fox brown quick The

Flowchart:

Flowchart: Java exercises: Reverse the content of a sentence without reverse every word.



For more Practice: Solve these Related Problems:

  • Write a Java program to reverse the characters of each word in a sentence while keeping the word order intact.
  • Write a Java program to reverse both the word order and the characters of each word in a sentence.
  • Write a Java program to reverse every alternate word in a sentence while maintaining the overall word order.
  • Write a Java program to reverse the order of words in a sentence, ensuring that punctuation remains in its original position.

Go to:


PREV : Multiply Without Operator.
NEXT : Longest Consecutive Sequence.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.