Answers for "how to sort 2d string array in c"

C
0

how to arrange a 2d array based on string length in c

1 #include<stdio.h>
  2 #include<string.h>
  3 void main()
  4 {
  5 char s[5][10];
  6 char t[10];
  7 int ele,i,j;
  8 ele=(sizeof(s)/sizeof(s[0]));
  9 printf("Enter the strings...n");
 10 for(i=0;i<ele;i++)
 11 scanf("%s",s[i]);
 12 for(i=0;i<ele;i++)
 13 printf("%sn",s[i]);
 14
 15 for(i=0;i<ele;i++)
 16 {
 17         for(j=0;j<ele-1-i;j++)
 18         {
 19                 if(strlen(s[j])>strlen(s[j+1]))
 20                         {       strcpy(t,s[j]);
 21                                 strcpy(s[j],s[j+1]);
 22                                 strcpy(s[j+1],t);
 23                         }
 24
 25
 26         }
 27 }
 28 printf("After sorting...n");
 29 for(i=0;i<ele;i++)
 30 printf("%s ",s[i]);
 31 }
~
Posted by: Guest on October-06-2021

Code answers related to "C"

Browse Popular Code Answers by Language