C program to Sort array of 10 strings

Below is C program to Sort array of 10 strings.

Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* 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;
}

No comments:

Post a Comment