zoukankan      html  css  js  c++  java
  • 字符串的交换——3种实现

    要知道,a[100]字符串的首地址与char *a 是不一样的,
    a[100]字符串的首地址不是指针,char *a就是指针!!!
    View Code
    //swap(char *a,char *b)
    #include<iostream>
    #include
    <string.h>
    using namespace std;

    void swap(char *a,char *b)
    {
    int al,bl,i;
    al
    =strlen(a)+1;
    bl
    =strlen(b)+1;
    if(al<bl)al=bl;

    int temp;
    for(i=0;i<al;i++)
    {

    temp
    =a[i];
    a[i]
    =b[i];
    b[i]
    =temp;
    }
    }

    void main()
    {
    char ap[20]="hello!";
    char bp[20]="how are you!";

    swap(ap,bp);
    cout
    <<ap<<endl;
    cout
    <<bp<<endl;
    }


    //swap(char* *a,char* *b)
    #include<iostream>
    using namespace std;

    void swap1(char* *a,char* *b)
    {
    char *temp;
    temp
    =*a;
    *a=*b;
    *b=temp;
    }

    void main()
    {
    char *ap="hello!";
    char *bp="how are you!";

    swap1(
    &ap,&bp);
    cout
    <<ap<<endl;
    cout
    <<bp<<endl;
    }

    //swap(char* &a,char* &b)
    #include<iostream>
    using namespace std;

    void swap1(char* &a,char* &b)
    {
    char *temp;
    temp
    =a;
    a
    =b;
    b
    =temp;
    }

    void main()
    {
    char *ap="hello!";
    char *bp="how are you!";

    swap1(ap,bp);
    cout
    <<ap<<endl;
    cout
    <<bp<<endl;
    }
  • 相关阅读:
    ac与ap同步分析
    ipsec原理(转载)
    Sublime Text自定制代码片段(Code Snippets)
    IPsec分析/测试/
    jQuery 学习
    windows pip 安装 转载
    转载 pep8安装
    转载别人的ftp,觉得目录结构不错,学习
    博客园 CSS 代码定制
    AC自动机
  • 原文地址:https://www.cnblogs.com/huhuuu/p/1977590.html
Copyright © 2011-2022 走看看