Reverse words in a given string
Given a String of length N reverse the whole string without reversing the individual words in it. Words are separated by dots.
Code:
package sap;
public class ReverseWords {
public static void main(String[] args){
String word = "I am done";
StringBuilder result = new StringBuilder("");
String[] arr = word.split("\\s");
for(int i=arr.length-1; i>=0; i--){
result.append(arr[i]);
result.append(" ");
}
System.out.println(result.toString());
}
}
Output:
done am I
Code:
package sap;
public class ReverseWords {
public static void main(String[] args){
String word = "I am done";
StringBuilder result = new StringBuilder("");
String[] arr = word.split("\\s");
for(int i=arr.length-1; i>=0; i--){
result.append(arr[i]);
result.append(" ");
}
System.out.println(result.toString());
}
}
Output:
done am I
Comments
Post a Comment