#include <stdio.h>
#define N 100
int mystrcmp(char *,char *); //string compare
void mystrcpy(char *,char *); //string copy
void myswap(char *, char *);
void sort2(char *, char *);
void sort3(char *, char *, char *);
int main(int argc, char *argv[])
{
char ch1[N],ch2[N],ch3[N];
char *ch_1,*ch_2,*ch_3;
printf("please input 3 line string.\n");
gets(ch1);
gets(ch2);
gets(ch3);
ch_1 = ch1;
ch_2 = ch2;
ch_3 = ch3;
sort3(ch_1,ch_2,ch_3);
printf("\nprintf sort 3 line string.\n");
puts(ch_1);
puts(ch_2);
puts(ch_3);
system("pause");
return 0;
}
int mystrcmp(char *ch1, char *ch2)
{
int i = 0;
while (*ch1 != '\0' && *ch2 != '\0')
{
if (*ch1++ == *ch2++)
{
continue;
}
else
{
i = *ch1++ - *ch2++;
break;
}
}
return i;
}
void mystrcpy(char *target, char *src)
{
while(*src)
{
*target++ = *src++;
}
*target = '\0';
}
void myswap(char *c1, char *c2)
{
char temp[N];
mystrcpy(temp,c1);
mystrcpy(c1,c2);
mystrcpy(c2,temp);
}
void sort2(char *c1, char *c2)
{
if (mystrcmp(c1,c2) > 0 )
{
myswap(c1,c2);
}
}
void sort3(char *c1, char *c2, char *c3)
{
sort2(c1,c2);
sort2(c1,c3);
sort2(c2,c3);
}
|