HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java Examples / Java program to reverse words in string without using functions

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?

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

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. Akash Barod

    July 21, 2020

    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);
        }
    }
  2. VISHAL BADAR

    October 11, 2019

    2 code is not proper,question is without using any function.But there is split function is used to solve the problem.

    • Jose

      April 2, 2020

      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.

Comments are closed on this article!

Search Tutorials

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

  • Sealed Classes and Interfaces