Skip to content

Instantly share code, notes, and snippets.

@otknoy
Created December 27, 2016 13:26
Show Gist options
  • Save otknoy/89bf656d334832a3759b82d3f50fc832 to your computer and use it in GitHub Desktop.
Save otknoy/89bf656d334832a3759b82d3f50fc832 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
void print_str(char *str[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
}
void strswap(char **s1, char **s2)
{
char *tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
void strsort(char *str[], int n)
{
int i, j;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (strcmp(str[i], str[j]) > 0) {
strswap(&str[i], &str[j]);
}
}
}
}
int main()
{
char *str[] = {"bbb", "aaa", "ddd", "ccc"};
print_str(str, NELEMS(str));
strsort(str, NELEMS(str));
print_str(str, NELEMS(str));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment