Program to arrange the string array alphabetically and display the result

PHOTO EMBED

Sat Jan 15 2022 04:36:58 GMT+0000 (Coordinated Universal Time)

Saved by @coduck #c #arrayof pointers to string #selectionsort

#include <String.h>
#include <stdio.h>
int main() {
  // initializing the pointer string array
  char *names[] = {"tree", "bowl", "hat", "mice", "toon"};
  char *temp; // temporary variable for swaping the values
  int i, j, a;
  printf("The names are:\n");
  for (i = 0; i < 5; i++)
    printf("%s\n", names[i]);
  // arranging names in alphabetically using selection sort
  for (i = 0; i < 5; i++) {
    for (j = i + 1; j < 5; j++) {
      // compares the two string and returns an integer value
      // if the value of a is greater than 0 then swapping begins
      a = strcmp(names[i], names[j]);
      
      if (a > 0) {
        temp = names[i];
        names[i] = names[j];
        names[j] = temp;
      }
    }
  }
  printf("The arranged names are:\n");
  for (i = 0; i < 5; i++)
    printf("%s\n", names[i]);
  return 0;
}
content_copyCOPY

Output:- The names are: tree bowl hat mice toon The arranged names are: bowl hat mice toon tree