Reverse the string InterviewBit Solution
Idea is to split string whenever a white space is detected , concat this arr in reverse manner to a string and return string without last blank space.
public class Solution {
public String reverseWords(String a) {
String arr[]=a.split("\\s");
String str=new String();
for(int i=arr.length-1;i>=0;i--)
{ str=str.concat(arr[i]);
str=str.concat(" ");
}
return str.trim();
}
}
public class Solution {
public String reverseWords(String a) {
String arr[]=a.split("\\s");
String str=new String();
for(int i=arr.length-1;i>=0;i--)
{ str=str.concat(arr[i]);
str=str.concat(" ");
}
return str.trim();
}
}
0 Comments:
Post a Comment