输入10个学生的姓名,再输入一个字符串,查找该字符串是否是某个学生的姓名,如果是,则输出是第几个(从1开始)学生的姓名,如果不是,则输出“It is not a name.” c++
时间: 2023-06-14 17:03:25 浏览: 62
以下是使用 C 语言实现的代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENT_NUM 10
int main()
{
char names[MAX_STUDENT_NUM][MAX_NAME_LEN]; // 存储学生姓名的数组
char search_name[MAX_NAME_LEN]; // 待查找的姓名
int i, found = 0; // found 表示是否找到待查找的姓名
// 输入学生姓名
printf("Please input %d student names:\n", MAX_STUDENT_NUM);
for (i = 0; i < MAX_STUDENT_NUM; i++) {
scanf("%s", names[i]);
}
// 输入待查找的姓名
printf("Please input a name to search:\n");
scanf("%s", search_name);
// 查找姓名
for (i = 0; i < MAX_STUDENT_NUM; i++) {
if (strcmp(names[i], search_name) == 0) { // 找到了待查找的姓名
found = 1;
printf("The name \"%s\" is the No.%d student's name.\n", search_name, i + 1);
break;
}
}
if (!found) { // 没有找到待查找的姓名
printf("It is not a name.\n");
}
return 0;
}
```
输入示例:
```
Please input 10 student names:
Alice Bob Charlie David Eric Frank Grace Henry Ivy Jack
Please input a name to search:
Frank
```
输出示例:
```
The name "Frank" is the No.6 student's name.
```
阅读全文