反转句子中的单词

来源:互联网 发布:mac上safari打不开网页 编辑:程序博客网 时间:2024/05/01 19:41
题目:单词翻转。输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变,句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如,输入“I am a student.”,则输出“student. a am I”。

public static void reverseWord( char[ ] s,int start,int end  ){

      char temp;
      while(start < end){ 
          temp = s[start];
          s[start] = s[end];
           s[end] = temp;
     }    

}

 public static void reverseSentense(char[] s){
              int len = s.length;
               int start = 0,end = 0;
              reverseWord(s,0,len-1);
           while(start < len){
                    if(s[start] == '  ' ){
                                  start++;
                                  end++;
                                 continue;
                           
                      }else if(s[end] == '  ' || end == len ]){
                                       reverseWord(s,start,end-1);
                                       strat = end;
                       }else{
                                end++;}
 
                    }


}



参考:http://www.cnblogs.com/remlostime/archive/2012/11/23/2784133.html
0 0