声明一个类 String,其数据成员为 char head[100],构造函数 String(char *Head)实现 head 的初始化,成员函数 void reverse()实现 head 内字...
时间: 2024-03-10 18:47:57 浏览: 25
好的,我了解你的问题。这是一个关于类的编程问题。以下是我的回答:
```cpp
class String {
private:
char head[100];
public:
String(char *Head) {
strcpy(head, Head);
}
void reverse() {
int len = strlen(head);
for (int i = 0; i < len / 2; i++) {
char temp = head[i];
head[i] = head[len - i - 1];
head[len - i - 1] = temp;
}
}
};
```
这是一个类 String,其中数据成员是 char 类型的 head 数组,大小为 100。构造函数 String(char *Head) 用于初始化 head 数组,成员函数 void reverse() 用于将 head 数组内的字符倒序。在 reverse() 函数中,我们先计算 head 数组的长度,并通过一个 for 循环将头尾两个字符交换位置,直到所有的字符都交换完毕。
希望我的回答对你有所帮助!
相关问题
用c++声明一个类 String,其数据成员为 char head[100],构造函数 String(char *Head)实现 head 的初始化,成员函数 void reverse()实现 head 内字符串的 逆序存放,成员函数
char* getHead() 返回 head 数组的地址。
这是一个简单的答案:
```
#include <cstring>
class String {
public:
char head[100];
String(char* Head) {
strcpy(head, Head);
}
void reverse() {
int len = strlen(head);
for (int i = 0, j = len - 1; i < j; i++, j--) {
char temp = head[i];
head[i] = head[j];
head[j] = temp;
}
}
char* getHead() {
return head;
}
};
```
声明一个类 String,其数据成员为 char head[100],构造函数 String(char *Head)实现 head 的初始化,成员函数 void reverse()实现 head 内字符串的 逆序存放,成员函数 void print()实现 head 内字符串的输出。
以下是实现该类的代码:
```
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char head[100];
public:
String(char *Head) {
strcpy(head, Head);
}
void reverse() {
int len = strlen(head);
for (int i = 0; i < len / 2; i++) {
swap(head[i], head[len - i - 1]);
}
}
void print() {
cout << head << endl;
}
};
```
该类的构造函数(`String(char *Head)`)用于初始化 `head` 数据成员,使其等于函数参数 `Head`。
成员函数 `reverse()` 用于将 `head` 中的字符串逆序存放。
成员函数 `print()` 用于将 `head` 中的字符串输出到控制台。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)