#include <stdio.h>
#include <ctype.h>
static char* next_word(char* s, char** e);
static char* swap_word(char* p1, char* e1, char* p2, char* e2, char** le);
char* sort_words_abc(char* s);
int main(void){
char s[] = "D python PHP COBOL Pascal ADA Lisp perl algol c basic APL Java";
puts(s);
puts( sort_words_abc(s) );
return 0;
}
//сортировка слов по-алфавиту(ASCII)
char* sort_words_abc(char* s){
char* a, *b, *f, *l, *p, *e;
a = next_word(s, &b);
if(a == NULL)
return s;
for(; *a; ){
p = a;
e = b;
f = b;
while((f = next_word(f, &l)) != NULL){
if(toupper(f[0]) < toupper(p[0])){
p = f;
e = l;
}
f = l;
}
if(p != a)
a = swap_word(a, b, p, e, &b);
else {
a = next_word(b, &b);
if(a == NULL)
break;
}
}
return s;
}
//выделение слова
static char* next_word(char* s, char** e){
char* p;
while(*s && !isalpha(*s))
++s;
p = s;
while(*p && isalpha(*p))
++p;
if(p != s){
*e = p;
return s;
}
return NULL;
}
//обмен слов
static char* swap_word(char* p1, char* e1, char* p2, char* e2, char** le){
char* p, c;
for(;p2 != e2; ++p2, ++p1, ++e1){
for(p = p2; p > p1; --p){
c = *p;
*p = *(p - 1);
*(p - 1) = c;
}
}
for(--e2; p1 != e1; --e1){
for(p = p1; p < e2; ++p){
c = *p;
*p = *(p + 1);
*(p + 1) = c;
}
}
*le = e1;
return p1;
}