Friday, 26 December 2014

Alphabetical Sort in C

Alphabetical sort is a program whereby strings of characters are placed in order based on the position of the characters on the conventional ordering of an alphabet.


Code:

#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);

for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
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 names \t Sorted Names \n");
printf("\n------------\n");
for(1=0; i<n; i++)
{
printf("%s \t \t %s \n");
}
printf("\n------------\n");
getch();
}

Input:
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project


Output:
-----------------------------------------
Input Names       Sorted names
-----------------------------------------
heap                     class
stack                    heap
queue                  object
object                  program
class                   project
program              queue
project                stack

------------------------------------------------------------------------------------------------------------------------