write a java program to reverse the words in the given String


package com.java2study;

public class StringWordsReverse {

public void revStrWords(String s){
String[] words = s.split(" ");
String wordreverse= "";
for(int i = words.length-1;i>=0;i--)
{
wordreverse += words[i]+" ";
}
System.out.println(wordreverse);

}
public static void main(String[] args) {
StringWordsReverse s = new StringWordsReverse();
s.revStrWords("Welcome to java2study");
}

}

Output

java2study to Welcome 

No comments:

Post a Comment