0% found this document useful (0 votes)
23 views

Program To Reverse Every Word in A String Using Methods

This Java program contains a method to reverse each word in a string by splitting the string into words, reversing each word, and joining the words back together with spaces. The method splits the input string, iterates through each word to reverse it character by character, and outputs the original and reversed strings.

Uploaded by

Mael Lopezjr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Program To Reverse Every Word in A String Using Methods

This Java program contains a method to reverse each word in a string by splitting the string into words, reversing each word, and joining the words back together with spaces. The method splits the input string, iterates through each word to reverse it character by character, and outputs the original and reversed strings.

Uploaded by

Mael Lopezjr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Program to reverse every word in a String using methods

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 BeginnersBook");
obj.reverseWordInMyString("This is an easy Java Program");
}
}
___

You might also like