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;
    }
  • 相关阅读:
    数据结构-图
    web.xml的运行顺序
    如何把自己打造成技术圈的 papi 酱
    也谈http中get和post
    手机充电速度及电池使用
    web项目Log4j日志输出路径配置问题
    JAVA模块化
    关于web安全
    Struts2中通配符
    2016第14周一
  • 原文地址:https://www.cnblogs.com/huhuuu/p/1977590.html
Copyright © 2011-2022 走看看