0
点赞
收藏
分享

微信扫一扫

c++字符串

sunflower821 2023-01-13 阅读 83


#include <iostream>

using namespace std;

int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

cout << "Greeting message: ";
cout << greeting << endl;

return 0;
}

Greeting message: Hello

#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
char str1[11] = "Hello";
char str2[11] = "World";
char str3[11];
int len ;

// 复制 str1 到 str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;

// 连接 str1 和 str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;

// 连接后,str1 的总长度
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;

return 0;
}

strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10


举报

相关推荐

0 条评论