Sie sind auf Seite 1von 3

C Program to Sort the N Names in an

Alphabetical Order
INFOLINKS_ONINFOLINKS_ON
.entry-meta
.entry-header
This C Program sorts the names in an alphabetical order. The program
accepts names & then sorts the names in an alphabetical order using string
operation.

Here is source code of the C program to sort the names in an


alphabetical order. The C program is successfully compiled and run on
a Linux system. The program output is also shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

/*
* C program to read N names, store them in the form of an array
* and sort them in alphabetical order. Output the given names and
* the sorted names in two columns side by side.
*/
#include <stdio.h>
#include <string.h>
void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;
printf("Enter the value of n \n");
scanf("%d", &n);
printf("Enter %d names n", \n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input NamestSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
}
$ cc pgm32.c
$ a.out
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
---------------------------------------Input Names Sorted names
-----------------------------------------heap
class
stack
heap
queue
object
object
program
class
project

program
queue
project
stack
------------------------------------------

Das könnte Ihnen auch gefallen