zoukankan      html  css  js  c++  java
  • 笔试题目“翻转字符串”的实现

    笔试题目

    //写一个函数,将字符串翻转,翻转方式如下:“I am a student”反转成“student a am I”,不借助任何库函数。


    据说这题在笔试或者面试当中,出现的频率非常高。刚好在书上也看到这题,又在博客园看到这题出现了(“发职位经典面试题”)。

    作者也提示了,方法是先反转整个字符串,然后再反转字串。譬如先将“I am a student”反转为“tneduts a ma I”,然后再对每个字串(空格分割)反转一次。思想就那么简单吧。实现起来的话,我就有点凌乱了。C++没学好。伤不起。

    算法

     1 #include <stdio.h>
     2 
     3 void main()
     4 {
     5     char str[]="I am a student";

    7 printf(str); 8 printf("\n"); 9 10 char *p,*q; 11 char temp; 12 p=q=str;14 while(*q!='\0') 15 { 16 q++; 17 } 18 q--; 19 while(p<=q) 20 { 21 temp=*p; 22 *p=*q; 23 *q=temp; 24 p++; 25 q--; 26 }//反转整个字符串 27 28 printf(str); 29 printf("\n"); 30 31 q=str;//指针指向开始位置 32 char *s,*t; 33 s=t=str; 34 while(*q!='\0') 35 { 36 if(*q==' ') 37 { 38 t--; 39 while(s<=t) 40 { 41 temp=*t; 42 *t=*s; 43 *s=temp; 44 s++; 45 t--; 46 }//反转局部字符串 47 48 s=q+1; 49 t=q; 50 }52 q++; 53 t++; 54 } 55 56 printf(str); 57 printf("\n"); 58 }

    改进

    运行之后,我发现是成功的。

    但是怎么想都感觉有点问题,把“I am a student”换成“you are a student”果然有问题。

    没有处理最后一个字串的缘故。因为我是按照

    if(*q==' ')

    来处理字串的,而字符串最后一个的结尾没有空格了,而是以'\0'结尾的。

    最后一个字串的处理我是这样做的。

    if(*q==' '||*(q+1)=='\0')
            {
                t--;
                if(*(q+1)=='\0')//处理最后一个字串
                    t++;

    看上去有点奇怪吧,但是确实是可以了。

    代码貌似可以继续优化吧。怎么都感觉自己写的代码好烂。

    代码

    以下是完整代码

    已处理最后字串的完整代码
    #include <stdio.h>
    
    void main()
    {
        char str[]="you are a student";
        printf(str);
        printf("\n");
    
        char *p,*q;
        char temp;
        p=q=str;
        while(*q!='\0')
        {
            q++;
        }
        q--;
        while(p<=q)
        {
            temp=*p;
            *p=*q;
            *q=temp;
            p++;
            q--;
        }//反转整个字符串
    
        printf(str);
        printf("\n");
    
        char *s;
        q=p=s=str;//指针指向开始位置
        while(*q!='\0')
        {
            if(*q==' '||*(q+1)=='\0')
            {
                p--;
                if(*(q+1)=='\0')//处理最后一个字串
                    p++;
                while(s<=p)
                {
                    temp=*p;
                    *p=*s;
                    *s=temp;
                    s++;
                    p--;
                }//反转局部字符串
    
                s=q+1;
                p=q;
            }
            q++;
            p++;
        }
    
        printf(str);
        printf("\n");
    }

     

    另外给一个我在《程序员面试宝典》看到的代码,不过这个主要采用数组处理,而且使用了库函数(strlen()),但是思想差不多吧。可以参考参考。

    《程序员面试宝典》实现方法
    #include <iostram>
    #include <stdio.h>
    
    int main(void)
    {
        int num=-12345,j=0,i=0,flag=0,begin,end;
        char str[]="I am a student",temp;
        j=strlen(str)-1;
        
        printf(" string=%s\n",str);
        //第一步是进行全盘反转,将单词变成“tneduts a ma I”
        while(j>i)
        {
            temp=str[i];
            str[i]=str[j];
            str[j]=temp;
            j--;
            i++;
        }
        printf(" string=%s\n",str);
        int i=0;
        //第二步进行部分反转,如果不是空格则开始反转单词
        while(str[i])
        {
            if(str[i]!=' ')
            {
                begin=i;
                while(str[i]&&str[i]!=' ')
                {
                    i++;
                }
                i=i-1;
                end=i;
            }
            while(end>begin)
            {
                temp=str[begin];
                str[begin]=str[end];
                str[end]=temp;
                end--;
                begin++;
            }
            i++;
        }
        printf(" string=%s\n",str);
        return 0;
    }

    既然看到了,就应该要思考吧。仅提升..

     

    参考
    《程序员面试宝典(第二版)》
    开发职位经典面试题 http://www.cnblogs.com/zhangjing230/archive/2012/05/17/2505711.html
  • 相关阅读:
    jquery validate自定义checkbox验证规则和样式
    【转】maven导出项目依赖的jar包
    maven web项目build失败
    【转】maven仓库快速镜像
    【转】javax.net.ssl.SSLHandshakeException(Cas导入证书)
    Maven发布web项目到tomcat
    tomcat启动是报Multiple Contexts have a path of "/XXX"
    spring无法扫描jar包的问题
    CAS单点登录之mysql数据库用户验证及常见问题
    搭建CAS单点登录服务器
  • 原文地址:https://www.cnblogs.com/rond/p/2505997.html
Copyright © 2011-2022 走看看