Using Java
Split the given inputString into words using split() method. Then take each individual word, reverse it and append to reverseString. Finally print reverseString. Below image shows code snippet of the same.
public class ReverseEachWord
{
static void reverseEachWordOfString(String inputString)
{
String[] words = inputString.split(" ");
String reverseString = "";
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);
}
reverseString = reverseString + reverseWord + " ";
}
System.out.println(inputString);
System.out.println(reverseString);
System.out.println("-------------------------");
}
public static void main(String[] args)
{
reverseEachWordOfString("Java Concept Of The Day");
reverseEachWordOfString("Java J2EE JSP Servlets Hibernate Struts");
reverseEachWordOfString("I am string not reversed");
reverseEachWordOfString("Reverse Me");
}
}
Output :
Java Concept Of The Day
avaJ tpecnoC fO ehT yaD
————————-
Java J2EE JSP Servlets Hibernate Struts
avaJ EE2J PSJ stelvreS etanrebiH sturtS
————————-
I am string not reversed
I ma gnirts ton desrever
————————-
Reverse Me
esreveR eM