Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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();
 }
Question Protected by Jamal
Tweeted twitter.com/#!/StackCodeReview/status/443879998077014016
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Better formatting.
Source Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 192

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();
 }
Source Link
bazang
  • 2.2k
  • 5
  • 23
  • 32
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /