我想知道如何按字母顺序打印两个字符串。 基本上要求用户给学生起名字,但他也需要给他们的姓氏。
我的问题是,如果名字相同,我怎么能按字母顺序打印它们而不必“重做”整个事情?
示例:
第一位学生: Mark Adele
第二名学生: Mark Boy
STRUCT:
typedef struct
{
int number;
char name[10];
char surname[10];
}num_nom_surname;
num_nom_surname *number_name_surname;
此部分只能比较名字
if (strcmp((number_name_surname[0].name), (number_name_surname[1].name)) < 0)
{
for (i = 0; i < number_of_students; i++)
{
printf("\n Student N%c %5d - ", 167, number_name_surname[i].number);
printf("Name: %s %s", number_name_surname[i].name, number_name_surname[i].surname);
}
}
else
{
for (i = number_of_students - 1; i >= 0; i--)
{
printf("\n Student N%c %5d - ", 167, number_name_surname[i].number);
printf("Name: %s %s", number_name_surname[i].name, number_name_surname[i].surname);
}
}
printf("\n Value of strcmp: %d", strcmp((number_name_surname[0].name), (number_name_surname[1].name)));