Java program to reverse words in string without using functions

In these java programs, learn to reverse the words of a string in Java without using api functions.

We can reverse the words of string in two ways:

  1. Reverse each word’s characters but the position of word in string remain unchanged.
    Original string : how to do in java
    Reversed string : woh ot od ni avaj 
    
  2. Reverse the string word by word but each word’s characters remain unchanged.
    
    Original string : how to do in java
    Reversed string : java in do to how
    

1. Reverse each word’s characters in string

In this example, we will follow below steps to reverse the characters of each word in it’s place.

Read string from input.
Split the string by using space as delimiter.
Loop through all words.
     – Reverse the characters of each word.
Print the final string.

package com.howtodoinjava.example;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Original string : ");

		String originalStr = scanner.nextLine();
		scanner.close();

		String words[] = originalStr.split("\\s");
		String reversedString = "";

		for (int i = 0; i < words.length; i++) 
		{
			String word = words[i];
			String reverseWord = "";
			for (int j = word.length() - 1; j >= 0; j--) {
				reverseWord = reverseWord + word.charAt(j);
			}
			reversedString = reversedString + reverseWord + " ";
		}

		// Displaying the string after reverse
		System.out.print("Reversed string : " + reversedString);
	}
}

Program output.

Original string : I love java programming
Reversed string : I evol avaj gnimmargorp 

2. Reverse the words in string

In this java program, we will reverse the string in such a way that each word’s characters are unchanged – but words are reversed in string by their position in the string.

package com.howtodoinjava.example;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Original string : ");

		String originalStr = scanner.nextLine();
		scanner.close();

		String words[] = originalStr.split("\\s");
		String reversedString = "";

		//Reverse each word's position
		for (int i = 0; i < words.length; i++) { 
            if (i == words.length - 1) 
            	reversedString = words[i] + reversedString; 
            else
            	reversedString = " " + words[i] + reversedString; 
        } 

		// Displaying the string after reverse
		System.out.print("Reversed string : " + reversedString);
	}
}

Program output.

Original string : I love java programming
Reversed string : programming java love I

Drop me your questions related to reverse each words in a string or reverse a sentence without using functions in Java.

Happy Learning !!

Leave a Reply

5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial