Code Scrappers


Sort set of strings in alphabetical C program


              #include<stdio.h>
              #include<stdlib.h>

              void main()
              {
                int i, j;

                 char str[20][20], temp[20];

                 printf("\n enter six strings : ");

                 for (i = 0; i < 6; i++)

                    scanf("%s", str[i]);

                 for (i = 1; i < 6; i++)
                 {
                      for (j = 1; j < 6; j++)
                      {
                          if (strcmp(str[j - 1], str[j]) > 0)
                          {
                              strcpy(temp, str[j - 1]);

                              strcpy(str[j - 1], str[j]);

                              strcpy(str[j], temp);
                          }
                      }
                 }

                 printf("\n sorted strings are : \n");

                 for (i = 0; i < 6; i++)
                 {
                    printf("\n%s", str[i]);
                  }
              }


          



Output

      enter six strings :

      shweta
      ritika
      saarthak
      arfat
      rishabh
      rishav

      sorted strings are :

      arfat
      rishabh
      rishav
      ritika
      saarthak
      shweta