zoukankan      html  css  js  c++  java
  • 经典算法面试题目-翻转一个C风格的字符串(1.2)

    题目:
    Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)

    写代码翻转一个C风格的字符串。(C风格的意思是”abcd”需要用5个字符来表示,包含末尾的 结束字符)

    解答:
    这道题如果就是要考察你有没有注意到C风格字符串最后的那个结束符,那我觉得还是像书 上写的那样,在代码中有所体现。代码如下:

    博主表示对这个第一种方法有点懵逼,有懂的大神麻烦教导一下,O(∩_∩)O谢谢。指针方面,我基本上没怎么学~~~

    void swap(char &a, char &b){
        a = a^b;
        b = a^b;
        a = a^b;
    }
    
    void reverse1(char *s){
        if(!s) return;
        char *p = s, *q = s;
        while(*q) ++q;
        --q;
        while(p < q)
            swap(*p++, *q--);
    }

    否则的话,可以直接获取字符串的长度,然后从两头开始一一交换相应的字符。代码如下:

    void swap(char &a, char &b)
    {
        a = a^b;
        b = a^b;
        a = a^b;
    }
    
    void reverse2(char *s)
    {
        int n = strlen(s);
        for(int i=0; i < n/2; ++i)
            swap(s[i], s[n-i-1]);
    }

    完整代码如下:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void swap(char &a, char &b){
        a = a^b;
        b = a^b;
        a = a^b;
    }
    
    void reverse2(char *s){
        int n = strlen(s);
        for(int i=0; i<n/2; ++i)
            swap(s[i], s[n-i-1]);
    }
    
    void reverse1(char *s){
        if(!s) return;
        char *p = s, *q = s;
        while(*q) ++q;
        --q;
        while(p < q)
            swap(*p++, *q--);
    }
    
    int main(){
        char s[] = "1234567890";
        reverse1(s);
        cout<<s<<endl;
        return 0;
    }

    我对第一种方法的理解:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    
    void swap(char &a, char &b){
        a = a^b;
        b = a^b;
        a = a^b;
    }
    
    void reverse2(char *s){
        int n = strlen(s);
        for(int i=0; i<n/2; ++i)
            swap(s[i], s[n-i-1]);
    }
    
    void reverse1(char *s){
        if(!s) return;
        printf("%d
    ",s);//s是一个地址
        char *p = s, *q = s;
        printf("%d
    ",p);
        printf("%d
    ",*p);//*p代表当前地址的一个字符
        while(*q){//遇到结束符的时候*p的值是0
            ++q;
            printf("p=%d*
    ",q);
            printf("*p=%d*
    ",*q);
            //最后一个是字符串的结束符
        }
        --q;
        printf("*%d*
    ",q);//此时的q为结束符之前的一个字符
        //此时q指向结束,p是指向开头
        while(p < q)//一直交换,直到中间位置
            swap(*p++, *q--);
    }
    
    int main(){
        char s[] = "abcdefghi";
        reverse1(s);
        cout<<s<<endl;
        return 0;
    }
    

    详细解释下c风格字符串:
    c风格的字符串是用数组存放的,一般要以’’结束
    而c++主要以string类代替,更加高效,且不易出错

    例如:string str = “123”;就是c++的风格;
    char str[4]=”123”;就是c风格
    string是c++中的关键字,和int,float等等一样, 在c++中,string 定义的变量可以直接存储字符串。在C语言中是没有这种直接存储字符串的变量的。
    [ ]里面是4,是因为,C风格的字符串,在最后都会默认添加’’,所以”123”本质上是1 2 3 ‘’
    它其实是占四个字节。所以如果你只定义3个存储空间的话,那’’这个字符串终止标志就回被丢弃,那么以后的应用就回出现问题。举个例子:你用strlen(str)试试查看长度为3时的这个字符串的长度,很可能就会有问题。 ‘’ 是c字符串都存在的。

  • 相关阅读:
    常用的VI/VIM命令
    那些年学过的一些算法
    huffman编码
    好用java库(一):java date/time api:jodatime
    linux启动
    ubuntu学习方式
    地址
    各种各样的软件
    jquery文件
    C变量与数据
  • 原文地址:https://www.cnblogs.com/webmen/p/5739105.html
Copyright © 2011-2022 走看看