-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Monday, July 6, 2020

Java Program To Reverse Words in a String

  • July 06, 2020
  • by

Java Program To Reverse Words in a String

In this Java Program, we will perform the reverse operation on the words in a string.

Logic : String[] words = str.split(" ");
             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 + " ";   
            }

Step 1 : Create class ReverseWords

Step 2 : Create methods reverseWordInMyString(String str)

Step 3 : Create an array of String named words and store the result of split method.
             str.split(" "); is a split method of string class which splits the string in several strings.

Step 4 : Create a variable reversedString = "";

Step 5 : Apply for loop in which variable i should be initialized to 0, i should be less than words.length and increment i.

Step 6 : In that for loop, create a variable word and store the result of words[i]. words[i] is the word from the for loop.

Step 7 : Create variable ReverseWord

Step 8 : Apply another for loop. Initialize j to word.length()-1, j should be greater than equal to 0 and decrement j.

Step 9 : Store the addition of the reverseWord and word.charAt(j) in the reverseWord variable.
The charAt(i) function returns the character at the given position in a string.

Step 10 : Close the for loop of j.

Step 11 : Store the result of reversedString + reverseWord + " " in the variable reverseString.

Step 12 : Close the for loop of i.

Step 13 : Print the String & reverseString.

Step 14 : Create the main method

Step 15 : Create the object of the ReverseWords Class

Step 16 : Call the method reverseWordInMyString of the ReverseWords Class.

Program :

public class ReverseWords 
    {
        public void reverseWordInMyString(String str)
        {
            //The split() method of String class splits string in several strings
            //based on the delimeter passed as an argument to it
            
            String[] words = str.split(" ");
            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--)
                {
                    //The charAt() function returns the character at the given position in a string
                    
                    reverseWord = reverseWord + word.charAt(j);
                    
                }
                reversedString = reversedString + reverseWord + " ";
                
            }
            System.out.println(str);
            System.out.println(reversedString);
            
        }
        public static void main(String[] args)
        {
            ReverseWords obj = new ReverseWords();
            obj.reverseWordInMyString("Welcome to my channel");
            obj.reverseWordInMyString("This is an easy Java Program");
            
        }
}

Output :

Welcome to my channel
emocleW ot ym lennahc 
This is an easy Java Program
sihT si na ysae avaJ margorP

For execution of the same do visit my YouTube Channel : 

A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages