Advertisement
cd62131

Sort String

Jan 31st, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int string_compare(const void *p1, const void *p2) {
  5.   return strcmp(*(char * const *) p1, *(char * const *) p2);
  6. }
  7. int main(void) {
  8.   const int n = 10;
  9.   char **a = (char **) malloc(n * sizeof(char *));
  10.   for (int i = 0; i < n; i++) {
  11.     a[i] = (char *) malloc(20);
  12.     printf("%d > ", i + 1);
  13.     scanf("%19s", a[i]);
  14.   }
  15.   qsort(a, n, sizeof(char *), string_compare);
  16.   for (int i = 0; i < n; i++)
  17.     printf("%d: %s\n", i + 1, a[i]);
  18.   return 0;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement