-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_sorting.c
More file actions
35 lines (33 loc) · 775 Bytes
/
string_sorting.c
File metadata and controls
35 lines (33 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <string.h>
int main()
{
char names[5][30];
int i = 0, j = 0;
char temp[30];
for (i = 0; i < 5; i++)
{
printf("enter name %d: ", i + 1);
fgets(names[i], 30, stdin);
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - 1 - i; j++)
{
if (strcmp(names[j], names[j + 1]) > 0)
{
strcpy(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
}
}
printf("----------------------\n");
printf(" Sorted Names Are: \n");
printf("----------------------\n");
for (i = 0; i < 5; i++)
{
printf("%d) %s", i + 1, names[i]);
}
return 0;
}