Search C Program

Loading...

Sort array of 10 strings

/* Sort array of 10 strings. */

#include<string.h>
#include<stdio.h>

int main()
{
 int i,j;
 char str[10][10],temp[10];
 printf("Type 10 names :\n");
 for(i=0;i<10;i++)
 {
  // gets(str[i]);
  // fgets is a better option over gets to read multiword string .
  fgets(str[i], 10, stdin);
 }
 for(i=0;i<10;i++)
 {
  for(j=0;j<10-1-i;j++)
  {
   if(strcmpi(str[j],str[j+1])>0)
   {
    strcpy(temp,str[j]);
    strcpy(str[j],str[j+1]);
    strcpy(str[j+1],temp);
   }
  }
 }
   
 printf("\nSorted Names :\n");
 for(i=0;i<10;i++)
  puts(str[i]);
 return 0;
}
 

1 comment: