.将字符数组a中的字符串“1 am a student.复制到字符数组b中,然后输出字符串b 要求: (1) 采用地址法实现; (2) 用指针变量来处理
时间: 2024-05-13 11:17:32 浏览: 3
采用地址法实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "I am a student.";
char b[20];
char *p1, *p2;
p1 = a;
p2 = b;
while (*p1 != '\0') {
*p2++ = *p1++;
}
*p2 = '\0';
printf("%s\n", b);
return 0;
}
```
用指针变量来处理:
```c
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "I am a student.";
char b[20];
char *p1, *p2;
p1 = a;
p2 = b;
while (*p1 != '\0') {
*p2 = *p1;
p1++;
p2++;
}
*p2 = '\0';
printf("%s\n", b);
return 0;
}
```
相关问题
用C++程序中定义了一个字符数组,输入该数组“I am a student.",然后输出
好的,以下是您要求的小程序,使用 C++ 语言编写:
```cpp
#include <iostream>
int main() {
const int max_length = 20; // 定义数组的最大长度
char str[max_length]; // 定义字符数组
std::cout << "请输入字符串:";
std::cin.getline(str, max_length); // 输入字符串
std::cout << str << std::endl; // 输出字符串
return 0;
}
```
程序中定义了一个字符数组 `str`,并设定其最大长度为 `max_length`。然后使用 `std::cin.getline()` 函数获取用户输入的字符串,将其存储在 `str` 数组中。最后使用 `std::cout` 输出该数组中的字符串,并添加换行符 `std::endl`。最终输出结果为:
```
请输入字符串:I am a student.
I am a student.
```
c语言将字符串a复制为宇符串b,然后输出字符串b。 二解题思路:定义两个字符数组a和b,用“I am a student.” 对a数组初始化。 ,今用地址法算出各元素的值
,将a数组中的值复制到b数组中,再输出字符串b即可。以下是示例代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char a[20] = "I am a student.";
char b[20];
// 将a数组中的值复制到b数组中
strcpy(b, a);
// 输出字符串b
printf("%s\n", b);
return 0;
}
```
输出结果为:
```
I am a student.
```
好了,接下来是一个笑话:为什么程序员总喜欢用黑色背景?因为黑色背景可以减少他们的眼睛疲劳啊!