Given an input string, reverse the string word by word.
For example: Given
s = "the sky is blue"
, return"blue is sky the"
.What constitutes a word?
- A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
- Yes. However, your reversed string should not contain leading or trailingspaces.
How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
public static String reverseWords(String s){
StringTokenizer strTok = new StringTokenizer(s, " ");
Stack<String> stack=new Stack<String>();
StringBuilder buff=new StringBuilder();
while(strTok.hasMoreElements()) {
String str = (String)strTok.nextToken();
if(!str.equals("")) stack.push(str);
}
while(!stack.isEmpty()){
buff.append(stack.pop());
if(!stack.isEmpty()) buff.append(" ");
}
return buff.toString();
}
Given an input string, reverse the string word by word.
For example: Given
s = "the sky is blue"
, return"blue is sky the"
.What constitutes a word?
- A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
- Yes. However, your reversed string should not contain leading or trailingspaces.
How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
public static String reverseWords(String s){
StringTokenizer strTok = new StringTokenizer(s, " ");
Stack<String> stack=new Stack<String>();
StringBuilder buff=new StringBuilder();
while(strTok.hasMoreElements()) {
String str = (String)strTok.nextToken();
if(!str.equals("")) stack.push(str);
}
while(!stack.isEmpty()){
buff.append(stack.pop());
if(!stack.isEmpty()) buff.append(" ");
}
return buff.toString();
}
Given an input string, reverse the string word by word.
For example: Given
s = "the sky is blue"
, return"blue is sky the"
.What constitutes a word?
- A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
- Yes. However, your reversed string should not contain leading or trailingspaces.
How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
public static String reverseWords(String s){
StringTokenizer strTok = new StringTokenizer(s, " ");
Stack<String> stack=new Stack<String>();
StringBuilder buff=new StringBuilder();
while(strTok.hasMoreElements()) {
String str = (String)strTok.nextToken();
if(!str.equals("")) stack.push(str);
}
while(!stack.isEmpty()){
buff.append(stack.pop());
if(!stack.isEmpty()) buff.append(" ");
}
return buff.toString();
}
Reversing words with white space requirements Reverse a string word by word
What would you think of this solution, for the problem below.
Given an input string, reverse the string word by word.
For example: Given
s = "the sky is blue"
, return"blue is sky the"
.What constitutes a word?
Given an input string, reverse the string word by word.
- A sequence of non-space characters constitutes a word.
For example, Given s = "the sky is blue", return "blue is sky the".
Could the input string contain leading or trailing spaces?
click to show clarification. Clarification:
- Yes. However, your reversed string should not contain leading or trailingspaces.
How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
What constitutes a word?
A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
public static String reverseWords(String s){
StringTokenizer strTok = new StringTokenizer(s, " ");
Stack<String> stack=new Stack<String>();
StringBuilder buff=new StringBuilder();
while(strTok.hasMoreElements()) {
String str = (String)strTok.nextToken();
if(!str.equals("")) stack.push(str);
}
while(!stack.isEmpty()){
buff.append(stack.pop());
if(!stack.isEmpty()) buff.append(" ");
}
return buff.toString();
}
Reversing words with white space requirements
What would you think of this solution, for the problem below.
Given an input string, reverse the string word by word.
For example, Given s = "the sky is blue", return "blue is sky the".
click to show clarification. Clarification:
What constitutes a word?
A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
public static String reverseWords(String s){
StringTokenizer strTok = new StringTokenizer(s, " ");
Stack<String> stack=new Stack<String>();
StringBuilder buff=new StringBuilder();
while(strTok.hasMoreElements()) {
String str = (String)strTok.nextToken();
if(!str.equals("")) stack.push(str);
}
while(!stack.isEmpty()){
buff.append(stack.pop());
if(!stack.isEmpty()) buff.append(" ");
}
return buff.toString();
}
Reverse a string word by word
Given an input string, reverse the string word by word.
For example: Given
s = "the sky is blue"
, return"blue is sky the"
.What constitutes a word?
- A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
- Yes. However, your reversed string should not contain leading or trailingspaces.
How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
public static String reverseWords(String s){
StringTokenizer strTok = new StringTokenizer(s, " ");
Stack<String> stack=new Stack<String>();
StringBuilder buff=new StringBuilder();
while(strTok.hasMoreElements()) {
String str = (String)strTok.nextToken();
if(!str.equals("")) stack.push(str);
}
while(!stack.isEmpty()){
buff.append(stack.pop());
if(!stack.isEmpty()) buff.append(" ");
}
return buff.toString();
}