工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1904|回复: 15

一道面试题(以单词为单位的反转)

[复制链接]
发表于 2006-6-16 19:30 | 显示全部楼层 |阅读模式
最近在考试,天天复习得无聊。今天上CSDN看到一道面试题,觉得自己可以做出来,就没有去看答案,结果做了很久才做出来。据说有四种方法,我还没有去看,如果你有兴趣,也做做,看我们这里的人可以做出多少种方法。

题目:

  1.                 以单词为最小单位翻转字符串
  2.                 Write the function String reverseStringWordByWord(String input)
  3.                 that reverses a string word by word.  For instance,
  4.                 reverseStringWordByWord("The house is blue") --> "blue is house The"
  5.                 reverseStringWordByWord("Zed is dead") --> "dead is Zed"
复制代码
发表于 2006-6-16 19:54 | 显示全部楼层
指向指针的指针……应该是一种办法
回复

使用道具 举报

 楼主| 发表于 2006-6-16 21:44 | 显示全部楼层
不太明,可以详细说明吗?
回复

使用道具 举报

 楼主| 发表于 2006-6-16 23:41 | 显示全部楼层
哎,没人做!我先贴出自己的做法!

void reverseWord(char*src,char*dest)
{
        int i,len;
        char*p;
        len=strlen(src);
        p=src+len-1; //指向最后一个字符
        *dest='\0'; //根据strcat()的特点,不得不如此
        for(i=0;i<len;i++)
                if((*(p-i))==' ')
                {
                        strcat(dest,(p-i+1));
                        strcat(dest," ");
                        if(i!=len-1)
                                *(p-i)='\0';
                }
                strcat(dest,p-len+1);
}
int main()
{
        char str[]="The house is blue";
        char a[strlen(str)+1];
        reverseWord(str,a);
        printf("%s\n",a);
        system("pause");
        return 0;
}

[ 本帖最后由 powerwind 于 2006-6-17 17:34 编辑 ]
回复

使用道具 举报

发表于 2006-6-17 10:42 | 显示全部楼层
  1. /*
  2. * 创建日期 2006-6-17
  3. * @author Woden Wang
  4. * power by Heatpixel.com
  5. */
  6. package test;

  7. public class StringReverse {

  8.         public static String reverseStringWordByWord(String input){
  9.                 String[] words = input.trim().split(" ");
  10.                 StringBuffer newWord = new StringBuffer();
  11.                 for(int i=words.length-1;i>=0;i--){
  12.                         newWord.append(words[i]);
  13.                         newWord.append(" ");
  14.                 }
  15.                 return newWord.toString().trim();
  16.         }
  17.        
  18.         public static void main(String[] args) {
  19.                 String input = "how do you do";
  20.                 System.out.println(reverseStringWordByWord(input));

  21.         }

  22. }
复制代码

[ 本帖最后由 wool王 于 2006-6-17 02:43 编辑 ]
回复

使用道具 举报

发表于 2006-6-17 14:49 | 显示全部楼层
单词全部是用空格分开还是可以带其它标点的呢???
回复

使用道具 举报

发表于 2006-6-17 16:25 | 显示全部楼层

  1. #include  <stdio.h>
  2. #define DIVI ' '
  3. /*分割符定义*/
  4. main()
  5. {
  6.   char *s="Welcome to GDUT!";
  7.   char *p[3];
  8.   char *tmp;
  9.   int i=0;
  10.   tmp=s;
  11.   while(*s!='\0')
  12.   {
  13.     if(*s++==DIVI)
  14.     {
  15.       p[i++]=tmp;
  16.       tmp=s;
  17.     }
  18.   }
  19.   p[i]=tmp;
  20.   for(;i>=0;i--)
  21.   {
  22.     while(*p[i]!=DIVI&&*p[i]!='\0')
  23.     {
  24.       putch(*p[i]);
  25.       p[i]++;
  26.     }
  27.     printf("\n");
  28.   }
  29.   getch();
  30.   return 0;
  31. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2006-6-17 17:34 | 显示全部楼层
原帖由 hjack 于 2006-6-17 14:49 发表
单词全部是用空格分开还是可以带其它标点的呢???


只有空格就可以啦.
因为后院搬家的原因,我回的一个帖不见了.现在补充:


WOOL用JAVA不算,呵呵!

如果我出题,一定要加上"不得使用厍函数,输入输出的测试除外"
回复

使用道具 举报

发表于 2006-6-17 18:35 | 显示全部楼层
楼上的……

strcat也是库函数的说……
回复

使用道具 举报

 楼主| 发表于 2006-6-17 22:01 | 显示全部楼层
既然楼上这样说,我就改一下!

  1. //相当于strlen()
  2. int getLength(char str[])
  3. {
  4.         int i=0;
  5.         while(str[i]!='\0')i++;
  6.         return i;
  7. }
  8. //相当于strcat
  9. void stringCat(char *a,char*b)
  10. {
  11.         int len,i=0;
  12.         len=getLength(a);
  13.         while (b[i]!='\0')
  14.         {
  15.                 a[len+i]=b[i];
  16.                 i++;
  17.         }
  18.         a[len+i]='\0';
  19. }
  20. //
  21. void reverseWord(char*src,char*dest)
  22. {
  23.         int i,len;
  24.         char*p;
  25.         len=strlen(src);
  26.         p=src+len-1;
  27.         *dest='\0';
  28.         for(i=0;i<len;i++)
  29.                 if((*(p-i))==' ')
  30.                 {
  31.                         stringCat(dest,(p-i+1));
  32.                         stringCat(dest," ");
  33.                         if(i!=len-1)
  34.                                 *(p-i)='\0';
  35.                 }
  36.                 stringCat(dest,p-len+1);
  37. }
  38. int main()
  39. {
  40.         char str[]="The house is blue";
  41.         char a[getLength(str)+1];
  42.         reverseWord(str,a);
  43.         printf("%s\n",a);
  44.         system("pause");
  45.         return 0;
  46. }
复制代码
回复

使用道具 举报

发表于 2006-6-18 00:10 | 显示全部楼层
我写的strcat

  1. char * myStrcat(char *des,char *src)
  2. {
  3. /*
  4.    The des's length MUST larger than
  5.    the total number of new string
  6. */
  7.   char *tmp=des;
  8.   while(*des++);
  9.   des--;
  10.   while(*src)
  11.   {
  12.     *des++=*src++;
  13.   }
  14.   *des='\0';
  15.   return tmp;
  16. }
复制代码
回复

使用道具 举报

发表于 2006-6-18 16:08 | 显示全部楼层
hjack,如果用标点也可以分词的话,我写的那段可以改成正则表达式分词。
回复

使用道具 举报

发表于 2006-6-18 16:09 | 显示全部楼层
原帖由 powerwind 于 2006/6/17 09:34 发表


只有空格就可以啦.
因为后院搬家的原因,我回的一个帖不见了.现在补充:


WOOL用JAVA不算,呵呵!

如果我出题,一定要加上"不得使用厍函数,输入输出的测试除外"


不用java的话我投降,哈哈。我已经被当时笔试的那些C++吓怕了。看到指针就头疼。
回复

使用道具 举报

发表于 2006-6-18 16:45 | 显示全部楼层
原帖由 wool王 于 2006-6-18 16:08 发表
hjack,如果用标点也可以分词的话,我写的那段可以改成正则表达式分词。



正则表达式……

不明,深奥……    上网查了下,看起来就晕,就像WOOL看指针一样

如果是标点分词的话,我的只要改掉宏 DIVI 就行了#define DIVI ','
回复

使用道具 举报

发表于 2006-6-18 20:18 | 显示全部楼层
php实现



  1. 1,利用已有的函数

  2. function reverseStringWordByWord( $sInput ) {
  3.   $aWords = explode( ' ', $sInput  ) ;
  4.   $aWordsReversed = array_reverse( $aWords ) ;
  5.   $sOutput = implode( ' ', $aWordsReversed ) ;
  6.   return $sOutput ;
  7. }

  8. 2,老老实实的写算法

  9. function reverseStringWordByWord( $sInput ) {
  10.   $sOutput = '' ;
  11.   $sWordTmp = '' ;
  12.   for( $i =  strlen( $sInput ) - 1; $i >= 0; $i-- ) {
  13.     if ( $sInput{ $i } == ' ' ) {
  14.        $sOutput .= $sWordTmp . ' ' ;
  15.        $sWordTmp = '' ;
  16.     }
  17.     else {
  18.       $sWordTmp = $sInput{$i} . $sWordTmp ;
  19.     }
  20.   }
  21.   $sOutput .= $sWordTmp ;  //the first word in the origin string

  22.   return $sOutput ;
  23. }

  24. 3,正则

  25. function reverseStringWordByWord( $sInput ) {
  26.   $sInput .= ' ' ; //construct for the regex search
  27.   $sOutput = '' ;
  28.   $sPatten = '/(\w+)\s+/i' ;
  29.   if ( preg_match_all( $sPatten, $sInput, $aMatches ) === false )  {
  30.     return false ;
  31.   }
  32.   else {
  33.     for( $i = count( $aMatches[1] ) - 1; $i >= 0; $i-- ) {
  34.       $sOutput .= ( $aMatches[1][$i] . ' ' ) ;
  35.     }
  36.     $sOutput = rtrim( $sOutput ) ;
  37.   }
  38.   
  39.   return $sOutput ;
  40. }

  41. 4,strtok的用法
  42. function reverseStringWordByWord( $sInput ) {
  43.   $sOutput = '' ;
  44.   $sWord = strtok( $sInput, " " );

  45.   while ( $sWord !== false ) {
  46.     $sOutput = $sWord . ' ' . $sOutput ;
  47.     $sWord = strtok(" ");
  48.   }
  49.   $sOutput = rtrim( $sOutput ) ;

  50.   return $sOutput ;
  51. }

复制代码

[ 本帖最后由 sasadong 于 2006-6-18 21:12 编辑 ]
回复

使用道具 举报

发表于 2006-6-22 00:16 | 显示全部楼层
shell实现


  1. #/bin/bash

  2. count=$#
  3. while [ $count -gt 0 ]
  4. do
  5.   cmd="echo ""$""$count"
  6.   eval $cmd
  7.   count=`expr $count -1`
  8. done

复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 07:18

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表