美文网首页
4.2 字符数组

4.2 字符数组

作者: 壹顾倾城 | 来源:发表于2020-01-09 10:40 被阅读0次
    • 程序来源 :C++ primer plus
    • 章 节 :4.2
    • 名 称 :string.cpp
    • 功 能 :输出字符串
    • 开发时间 :2019-10-29
    • 版 本 :v1.0
    • 运行测试 :通过
    /********************************
     * 程序来源 :C++ primer plus
     * 章    节 :4.2
     * 名    称 :string.cpp
     * 功    能 :输出字符串
     * 开发时间 :2019-10-29
     * 版    本 :v1.0
     * 运行测试 :通过
     *******************************/
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
        const int Size = 15;
    
        char name1[Size];                //size: 15
        char name2[Size] = "C++owboy";   //size: 15, len:8
    
        cout << "Howboy I'm " << name2;
        cout << "! what's you name?\n";
        cin >> name1;                   //basicman
        cout << "well," << name1 <<",your name has ";   //basicman
        cout << strlen(name1) <<" letters and is stored\n";  //8
        cout <<"in an array of " << sizeof(name1) <<" bytes.\n";  //15
        cout <<"You initial is "<< name1[0] <<".\n";  //b
        name2[3] = '\0';    // end name2  name2[]="C++\0"
        cout << "Here are he first 3 characters of my name ";
        cout << name2 << endl;   //C++
    
        return 0;
    }
    

    运行结果:

    Howboy I'm C++owboy! what's you name?
    BasicMan
    well,BasicMan,your name has 8 letters and is stored
    in an array of 15 bytes.
    You initial is B.
    Here are he first 3 characters of my name C++
    
    --------------------------------
    Process exited after 6.358 seconds with return value 0
    请按任意键继续. . .
    

    相关文章

      网友评论

          本文标题:4.2 字符数组

          本文链接:https://www.haomeiwen.com/subject/dhcyactx.html