c++ 字符串

#include<iostream>
#include<cstring>
using namespace std;
void strarr();
void strstring();
int main(){
    strarr();
    return 0;
}
void strstring(){
    string str1="runoob";
    string str2="google";
    string str3;
    int len;
    //复制字符串
    str3=str1;
    cout << "str3:"<<str3<<endl;
    //链接字符串
    str3=str1+str2;
    cout << "str3:"<<str3<<endl;
    //计算总长度
    len=str3.size();
    cout << "总长度 str3.size()" <<len<<endl;
    // 输出如下
    // str3:runoob
    // str3:runoobgoogle
    // 总长度 str3.size()12
}

void strarr(){
    char str1[13]="runoob";
    char str2[13]="google";
    char str3[13];
    int len;
    //复制字符串 str1 到str3
    strcpy(str3,str1);
    cout << "str3: " << str3 << endl;
    //链接字符串 str1在前,str2 在后
    strcat(str1,str2);
    cout <<"strcat(str2,str1)"<<str1<<endl;
    //计算字符串总长度
    len=strlen(str1);
    cout << "strlen(str1)"<<len<<endl;

    //输出如下
    // str3: runoob
    // strcat(str2,str1)runoobgoogle
    // strlen(str1)12
}

 

posted on 2025-06-28 20:53  running-fly  阅读(6)  评论(0)    收藏  举报

导航