reverse a string

Java Program to reverse words in a String with Example

This program reverses every word in a string and outputs the reversed string. For example, if we enter a string as “Reverse the word of this string,” the program will return: “esrever eht drow fo siht gnirts.”

In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.

Example: To reverse every word in a String using methods

In this program, we first use the split() method to split the given string into substrings. The substrings are stored as words in a String array. The program then uses a reverse for loop to reverse each word of the substring.

public class Example
{
   public void reverseWordInMyString(String str)
   {
	/* The split() method of String class splits
	 * a string in several strings based on the
	 * delimiter 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) 
   {
	Example obj = new Example();
	obj.reverseWordInMyString("Welcome to Developersdome");
	obj.reverseWordInMyString("This is Java Program");
   }
}

Output:

Welcome to Developersdome
emocleW ot emodsrepoleveD
This is Java Program
sihT si avaJ margorP

reverses every word in a string reverses every word in a string reverses every word in a string reverses every word in a string reverses every word in a string reverses every word in a string reverses every word in a string reverses every word in a string reverses every word in a string

gJava Basic Syntax | Java Tutorial

Leave a Reply