论坛首页 招聘求职论坛

讨论曾经被考过的一个面试题:将一个英文句子单词倒置的实现...

浏览 39184 次
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-09-07  
嘿嘿, 看来还是偶的答案最完美
Readonly 写道

google
java reverse string


然后第2个link里面就有答案:
Reverse string (by words)

http://www.idevelopment.info/data/Programming/java/strings/StringReverseWord.java

满分, hoho.

p.s, tnnd, 竟然在这里讨论技术......, 踢飞到技术区
0 请登录后投票
   发表时间:2004-09-07  
Original string: Alex,Michael Hunter
Reverse word string: Hunter Alex,Michael

这个程序和gigix的本质是一样的,不能给满分!
0 请登录后投票
   发表时间:2004-09-07  
正则表达式的\b表示的是word boundary,可堪此任。
假定要求执行效率最高的代码呢,。。。

题外话:昨天经人介绍了一个叫swig的东西(swig.sourceforge.net),用来将C/C++代码移植到python,ruby,perl,java,lisp等中。对于java也应该是对JNI的包装吧。试了python的还不错。
0 请登录后投票
   发表时间:2004-09-08  
综合了一下,最好的实现

public class StringReverseWord {

    private static void doStringReverseWord(); {
        String SEPARATORS = " ,\t:'';?"; 
        String a = "Alex Michael,Hu'nter?kk";
        Stack  stack = new Stack();;
        StringTokenizer tempStringTokenizer = new StringTokenizer(a,SEPARATORS,true);;

        while (tempStringTokenizer.hasMoreTokens();); {
            stack.push(tempStringTokenizer.nextElement(););;
        }

        System.out.println("\nOriginal string: " + a);;

        System.out.print("Reverse word string: ");;
        while(!stack.empty();); {
            System.out.print(stack.pop(););;            
        }
        
    }


    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args); {
        doStringReverseWord();;
    }

}
0 请登录后投票
   发表时间:2004-09-08  
to 凤舞凰扬:
     没有夸张,如果用你的办法,逗号之类的间隔符号虽然被去掉了,却没有象庄表伟要求的那样被打印出来;

to sankxuan:
    用了StringTokenizer(a,SEPARATORS,true)的三参数的构造函数,漂亮,确实是最好的实现。

------------------------------------------------------
重构了一下,下面是重构后的代码和执行结果:


import java.util.StringTokenizer;

public class Test{

    public static final String SEPARATORS = " ,\t:'';?!"; 

    public static String reverse(String input);{
    
        StringTokenizer st = new StringTokenizer(input, SEPARATORS, true);;
        StringBuffer words = new StringBuffer("");;
        while (st.hasMoreTokens();); {
            words.insert( 0, st.nextToken(); );;
        }    
        return words.toString();;
    }        
    
    public void testReverse();{
        
        String[] sentences = new String[]{
            "Hello, world!",
            "I am a student",
            "Am I a student? yes, or no",
            "Am I a student ? yes , or no",
            "Zhuang says:'It's just a coding game.'"
            };
            
        for (int i = 0; i < sentences.length; i++);
            System.out.println("Sentence[" + i + "]=[" + sentences[i]+"], " + 
                "After reversed: [" + Test.reverse(sentences[i]);+"]");;
       
    }
    
    public static void main(String[] args);{
        new Test();.testReverse();;
    }
    
        
}



--------------------------------------------------------------------------

执行结果:


引用
>ant exec

Buildfile: build.xml

exec:
     [java] Sentence[0]=[Hello, world!], After reversed: [!world ,Hello]
     [java] Sentence[1]=[I am a student], After reversed: [student a am I]
     [java] Sentence[2]=[Am I a student? yes, or no], After reversed: [no or ,yes ?student a I Am]
     [java] Sentence[3]=[Am I a student ? yes , or no], After reversed: [no or , yes ? student a I Am]
     [java] Sentence[4]=[Zhuang says:'It's just a coding game.'], After reversed: ['game. coding a just s'It':says Zhuang]
0 请登录后投票
   发表时间:2004-09-08  
Very Good.

结帖。
0 请登录后投票
   发表时间:2004-09-09  
不好意思续一下貂
org.apache.commons.lang.StringUtils
public static String[] split(String source,String trim)
0 请登录后投票
   发表时间:2004-09-09  
to sevenbamboos:

无论是java.util.String 中的split() , 还是org.apache.commons.lang.StringUtils 的
public static String[] split(String source,String trim),虽然功能很强大,但都没有提供 StringTokenizer 中三参数的构造函数功能,这个构造函数解释如下:

public StringTokenizer(String str, String delim, boolean returnDelims)
If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.

但这个题目要求的恰好是“delimiter characters are also returned as tokens”;
0 请登录后投票
   发表时间:2004-09-10  
StringTokenizer , 强~

正则表达式的\b是零字符匹配
0 请登录后投票
   发表时间:2004-09-12  
学习了一下,决定下次遇到这样回答
import java.util.StringTokenizer;

public final class StringReverser {
  
  public final static String DEFAULT_DELI = " ,\t:'';?!"; 
  public final static boolean DEFAULT_INCLUDE_DELT = true;
  private StringTokenizer tokenizer = null;
  
  public StringReverser(String source); {
    this(source,DEFAULT_DELI,DEFAULT_INCLUDE_DELT);;
  }
  public StringReverser(String source,String delimiters); {
       this(source,delimiters,DEFAULT_INCLUDE_DELT);;
  }
  public StringReverser(String source,String delimiters,boolean includeDelimiter); {
       tokenizer = new StringTokenizer(source,delimiters,includeDelimiter);;	
  }

  public String reverse(); {
       StringBuffer result = new StringBuffer();;
       while(tokenizer.hasMoreTokens(); ); {
          result.insert(0,tokenizer.nextToken(); );;
       }
       return result.toString();;
  }
}
0 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics