store names and arrange them in ascending order in c

Solutions on MaxInterview for store names and arrange them in ascending order in c by the best coders in the world

showing results for - "store names and arrange them in ascending order in c"
Dianne
27 Jan 2020
1#include <stdio.h>
2#include <string.h>
3
4void main()
5{
6	char name[10][20], temp_name[10][20], temp[20];
7    int i, j, n;
8
9    printf("Only the first letter of the name should be in uppercase\n\n");
10    printf("enter number of names you want to input: ");
11    scanf("%d", &n);
12
13    for (i = 0; i < n; i++)
14    {
15        printf("Enter name %d: ", i +1);
16        scanf("%s", &name[i]);
17        strcpy(temp_name[i], name[i]);
18    }
19
20    for (i = 0; i < n - 1 ; i++)
21    {
22        for (j = i + 1; j < n; j++)
23        {
24            if (strcmp(name[i], name[j]) > 0)
25            {
26                strcpy(temp, name[i]);
27                strcpy(name[i], name[j]);
28                strcpy(name[j], temp);
29            }
30        }
31    }
32
33
34    printf("\n\nSorted names in ascending alphabetical order\n\n");
35
36	for (i = 0; i < n; i++)
37    {
38            printf("%s\n", name[i]);
39    }
40}
41