String sorting c program compare words alphabetically . Easy sorting method used in this program.
Program :
Output :
Program :
/* STRING SORT */ #include<stdio.h> #include<conio.h> #include<string.h> #define ITEMS 5 /* defines preprocessor macro */ #define MAXCHAR 20 void main() { char st[ITEMS][MAXCHAR],dummy[MAXCHAR]; int i=0,j=0; clrscr(); printf("\n\t Enter names %d items \n",ITEMS); while(i<ITEMS) { scanf("\t%s",&st[i++]); } for(i=0;i<ITEMS-1;i++) { for(j=i+1;j<ITEMS;j++) { if(strcmp(st[i],st[j])>0) { strcpy(dummy,st[i]); strcpy(st[i],st[j]); strcpy(st[j],dummy); } } } printf(" \n\t Alphabetiacl list\n"); for(i=0;i<ITEMS;i++) { printf("\t%s \n",st[i]); } getch(); exit(0); }
Output :