strcpy和strlen对字符串的直接使用的例子c加加
时间: 2024-02-25 08:51:25 浏览: 15
C++ 中的 `strcpy` 和 `strlen` 函数与 C 语言中的用法基本相同,下面是 C++ 中使用 `strcpy` 和 `strlen` 函数对字符串的操作的例子:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[20];
char str2[20];
strcpy(str1, "Hello");
strcpy(str2, "World");
strcat(str1, str2);
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
int len1 = strlen(str1);
int len2 = strlen(str2);
cout << "Length of str1 = " << len1 << endl;
cout << "Length of str2 = " << len2 << endl;
return 0;
}
```
运行结果为:
```
str1: HelloWorld
str2: World
Length of str1 = 10
Length of str2 = 5
```
这里 `cout` 是 C++ 中的输出流,`endl` 是 C++ 中的换行符,`strlen` 函数用于获取字符串的长度。其他部分与 C 语言中的例子基本相同。