使用C语言编写查找年龄为10的学生,打印其姓名(按值查找) 6.删除第二个学生的信息(删除),打印删除后的学生信息表
时间: 2024-03-18 17:41:17 浏览: 13
好的,针对您的问题,我可以给出以下代码示例:
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int age;
};
int main() {
struct Student students[5] = {
{"Tom", 12},
{"Jerry", 10},
{"Alice", 9},
{"Bob", 10},
{"Eva", 11}
};
// 查找年龄为10的学生
printf("Students whose age is 10:\n");
for (int i = 0; i < 5; i++) {
if (students[i].age == 10) {
printf("%s\n", students[i].name);
}
}
// 删除第二个学生
for (int i = 2; i < 5; i++) {
strcpy(students[i - 1].name, students[i].name);
students[i - 1].age = students[i].age;
}
// 打印删除后的学生信息表
printf("Student information after deletion:\n");
for (int i = 0; i < 4; i++) {
printf("%s, %d\n", students[i].name, students[i].age);
}
return 0;
}
```
以上代码中,我们定义了一个结构体 `Student` 表示学生,包含姓名和年龄两个属性。我们创建了一个长度为5的 `students` 数组,存储了5个学生的信息。
首先,我们查找年龄为10的学生,遍历整个 `students` 数组,如果发现某个学生的年龄是10,我们就打印出其姓名。
接着,我们删除第二个学生的信息,即将第3个至第5个学生的信息整体向前移动一位。我们使用 `strcpy` 函数将学生姓名复制到前一个学生的位置,将年龄赋值给前一个学生的年龄属性。这样,原来的第二个学生的信息就被覆盖了。
最后,我们打印删除后的学生信息表,即遍历前4个元素并打印出学生姓名和年龄。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)