program to sort the n names in an alphabetical order

2
#include <stdio.h> #include <string.h> int main() { char name[20][10], tname[20][10], temp[10]; int i, j, n; printf("Enter the number of Names you want to do input:\n"); //how many names input scanf("%d", &n); printf("Enter names: \n"); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } //input names for (i = 0; i < n - 1 ; 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); } } } //for alphabetically sorting

Upload: samsil-arefin

Post on 22-Jan-2017

30 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Program to sort the n names in an alphabetical order

#include <stdio.h>

#include <string.h>

int main()

{

char name[20][10], tname[20][10], temp[10];

int i, j, n;

printf("Enter the number of Names you want to do input:\n"); //how many names input

scanf("%d", &n);

printf("Enter names: \n");

for (i = 0; i < n; i++)

{

scanf("%s", name[i]);

strcpy(tname[i], name[i]);

} //input names

for (i = 0; i < n - 1 ; 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);

}

}

} //for alphabetically sorting

Page 2: Program to sort the n names in an alphabetical order

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 storted names

}

printf("------------------------------------------\n");

}

By Samsil Arefin

[email protected]