[ create a new paste ] login | about

Link: http://codepad.org/Ljq8XRdE    [ raw code | fork ]

C, pasted on Jan 18:
#include <stdio.h>
#include <string.h>

size_t slen(const char *s)
{
  const char *p = s;

  while (*s)
    ++s;
  return s - p;
}

int scoll(const char *s1, const char *s2)
{
  while (*s1 == *s2) {
    if (*s1 == 0)
      return 0;
    ++s1, ++s2;
  }
  return (unsigned char)*s1 - (unsigned char)*s2;
}

int main(int ac, char **av)
{
  int i, j, k, a;
  char *s;
  size_t l1, l2;

  for (i = 2; i < ac; ++i) {
    for (j = 1; j < i; ++j) {
      l1 = slen(av[i]);
      l2 = slen(av[j]);
      if (l1 == l2) {
	a = scoll(av[i], av[j]);
      } else if (l1 < l2) {
	a = 1;
      } else {
	a = -1;
      }
      if (a < 0) {
	s = av[i];
	for (k = i - 1; k >= j; --k)
	  av[k + 1] = av[k];
	av[j] = s;
	break;
      }
    }
  }
  for (i = 1; i < ac; ++i) {
    printf("%s\n", av[i]);
  }
  return 0;
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: