Frequently Asked Java Program 18: Java Program to Reverse Every Word of a String
Hello Programmers,
“Java Program to Reverse Every Word of a String” is frequently asked programming interview questions to freshers and experienced. I will try here to explain logic in simple steps so that you do not need to remember lines of code. Just implement the logic and you can code it.
Problem statement:
Original string :Make Selenium Easy
String with reversed words :ekaM muineleS ysaE
We need to reverse a given string word by word. Note here we are not changing default position of words here.
Solution:
Step 1: First of all we need to extract words from a given string. Two words are separated by while space. We can use split() method given by String class which splits a string by provided delimiter and returns a String[] of splitted words. For example:
[java]
public class SplitString {
public static void main(String[] args) {
String inputString = “Make Selenium Easy”; String spliited[] = inputString.split(” “); for(int i=0;i