由键盘任意输入10个姓名,再输入一个姓名,在此10个姓名中查找此姓名是否存在;如果存在,输出其位置的C语言程序
时间: 2023-06-09 19:08:17 浏览: 70
#include <stdio.h>
#include <string.h>
#define MAX_NAMES 10
#define MAX_NAME_LENGTH 50
int main() {
char names[MAX_NAMES][MAX_NAME_LENGTH];
int i, position = -1;
char search_name[MAX_NAME_LENGTH];
// input 10 names
printf("Please input 10 names:\n");
for (i = 0; i < MAX_NAMES; i++) {
scanf("%s", names[i]);
}
// input the name to search
printf("Please input a name to search:\n");
scanf("%s", search_name);
// search the name in the names array
for (i = 0; i < MAX_NAMES; i++) {
if (strcmp(names[i], search_name) == 0) {
position = i;
break;
}
}
// output the result
if (position == -1) {
printf("The name '%s' does not exist in the names array.\n", search_name);
} else {
printf("The name '%s' is at index %d in the names array.\n", search_name, position);
}
return 0;
}
阅读全文