In this tutorial, we are going to write a C Program to sort a customer list in the alphabetical list in C Programming with practical program code and step-by-step full complete explanation.
C Program to sort a customer list in the alphabetical list.
#include<stdio.h> #include<conio.h> #define CUSTOMERS 10 void main() { char first_name[20][10], second_name[20][10], surname[20][10], name[20][20], telephone[20][10], dummy[20]; int i,j; printf("Input names and telephone numbers"); printf("?"); for(i=0;i<CUSTOMERS;i++) { scanf("%s %s %s %s", first_name[i],second_name, surname[i], telephone[i]); strcpy(name[i], surname[i]); strcat(name[i], ","); dummy[0]=first_name[i][0]; dummy[1]=' '; strcat(name[i],dummy); stcat(name[i],"."); dummy[0]=second_name[i][0]; dummy[1]=' '; strcat(name[i],dummy); } for(i=1;i<=CUSTOMERS-1;i++) for(j=1;j<=CUSTOMERS-1;j++) if(strcmp(name[j-1], name[j])>0) { strcpy(dummy,name[j-1]); strcpy(name[j-1],name[j]); strcpy(name[j],dummy); strcpy(dummy,telephone[j-1]); strcpy(telephone[j-1],telephone[j]); strcpy(telephone[j],dummy); } printf("CUSTOMERS LIST IN ALPHABETICAL ORDER"); for(i=0;i<CUSTOMERS;i++) printf("%-20st%-10s",name[i],telephone[i]); }
Output
Input names and telephone numbers ? anandamurugan 9486153102 Renukadevi 9486644542 CUSTOMERS LIST IN ALPHABETICAL ORDER anandamurugan 9486153102 Renukadevi 9486644542