zoukankan      html  css  js  c++  java
  • 使用多种方法编写将两个字符串连接在一起的程序-简单

    源程序:

    第一种方法:

    #include <iostream>

    #include <string>

    using namespace std;

    int main()

    {

      string str1="i am ";

      string str2="a teacher.";

      cout<<"连接前:"<<endl;

      cout<<"str1:"<<str1.data()<<endl;

      cout<<"str2:"<<str2.data()<<endl;

      str1.append(str2);

      cout<<endl;

      cout<<"连接后:";

      cout<<"str1:"<<str1.data()<<endl;

      cout<<"str2:"<<str2.data()<<endl;

       return 1;

    }

    第二种方法:

    #include < iostream >

    #include < string >

    using namespace std;

    void main()

    {

      //使用string 类定义字符串,完成字符串连接

      string str1="C++",str2="程序设计";

      //string str1("C++"),str2("程序设计");

      string str3;

      str3 = str1+str2;//连接方式1

      cout<< str3<< endl;

      //使用char 数组定义字符串,完成连接

      char c1[] = {"c++"},c2[] = {"program"};

      char c3[20];

      int i=0,k=0;

      for(i=0;i<20;i++)//初始化c3

        c3[i]='';

      i=0;

      while(c1[i]!='')

      {

        c3[k]=c1[i];

        i++;

        k++;

      }

      i=0;

      while(c2[i]!='')

      {

        c3[k]=c2[i];

        i++;

        k++;

      }

      cout<< c3<< endl;

    }

    运行结果:

  • 相关阅读:
    从 http 升级到 https 过程中遇到的一些问题
    Java 对象,数组 与 JSON 字符串 相互转化
    jQuery 事件探秘
    eclipse 添加 hibernate 插件
    struts2 + jquery + json 简单的前后台信息交互
    匿名类、匿名方法、扩展方法
    禁用右键
    JS聊天室
    MVC知识汇总
    知识点汇总
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11887623.html
Copyright © 2011-2022 走看看