Sorting an array by using strcmp
OutPut:
#include<stdio.h>
#include<string.h>
#include<string.h>
main()
{
char a[4][10],temp[10];
int i,j,k;
printf("enter 4 strings\n");
for(i=0;i<4;i++)
scanf("%s",a[i]);
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
printf("sorted array is:\n");
for(i=0;i<4;i++)
printf(".....%s....\n",a[i]);
}
madan@madan-Lenovo-G570:~/madan$ vi arr_sort.c madan@madan-Lenovo-G570:~/madan$ cc arr_sort.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter 4 strings welcome madan tech world sorted array is: .....madan.... .....tech.... .....welcome.... .....world....