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

C
2

2d array of strings in c

language[0] => "Java";
language[1] => "Python";
language[2] => "C++";
language[3] => "HTML";
language[4] => "SQL";
Posted by: Guest on December-10-2020
1

2d array of strings in c

// it is valid
char language[ ][10] = {"Java", "Python", "C++", "HTML", "SQL"};
Posted by: Guest on December-10-2020
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("%s\n",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 "how to arrange a 2d array based on string length in c"

Code answers related to "C"

Browse Popular Code Answers by Language