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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “Java program to reverse words in string without using functions”

  1. Here is my code for this question: without using split or reverse functions :

    import java.util.ArrayList;
    import java.util.Scanner;
    
    class ReverseWordSentence {
    
    public static void main(String[] args) 
    {
        System.out.println("Enter the phrase to be reversed: ");
    
        Scanner sc = new Scanner(System.in);
        String phrase = sc.nextLine();
        char[] ch = phrase.toCharArray();
    
        ArrayList<Character> wdl = new ArrayList<Character>();
    
        for(int i=0; i&lt;ch.length; i++) {
            if(String.valueOf(ch[i]).equals(" ") || (i == phrase.length()-1) ) {
                if (i == phrase.length()-1) {
                    wdl.add(ch[i]);
                }
    
                for(int j=wdl.size()-1; j>-1;--j) {
                    System.out.print(wdl.get(j));
                }
    
                if(ch[i]==' ') {
                    System.out.print(" ");
                }
    
                wdl.clear();
            }
            else
            {
                wdl.add(ch[i]);
            }
        }
    }
    }
    
    Reply
  2. This is one is much easy…..

    package com.coding;
    import java.util.Scanner;
    
    public class Java 
    {
        public static void main(String[] args) 
        {
        	String str="welcome to java akash";
            String words[] = str.split(" ");
            String reversedString = "";
     
            //Reverse each word's position
            
            for (int i = 0; i < words.length; i++) 
            {   
                    reversedString = " " + words[i] + reversedString; 
            } 
     
            // Displaying the string after reverse
            System.out.print("Reversed string : " + reversedString);
        }
    }
    Reply
    • That is quite true. BUT, the author referred to not using any IMPORTED CLASS Function. Of course, he had to use the Scanner class so the user can interact with the program. The .split() function is a base function that doesn’t belong in any imported class. The author used a function that is within Vanilla Java.

      This code works well. This code is proper.

      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.