#include
#include
#include
#define LIM 5
#define SIZE 32
int read(char (*p)[SIZE],int);
void d_m(void); /*display menu*/
void output(char (*p)[SIZE],int);
void s_a(char (*p)[SIZE],int); /*sorted by ASCII*/
void s_l(char (*p)[SIZE],int); /*sorted by length*/
void s_l_f(char (*p)[SIZE],int); /*sorted by length of first word*/
int main(void)
{
char array[LIM][SIZE];
int count;
char ch;
int n;
n=read(array,LIM);
while(1)
{
if(n
d_m();
ch=getchar();
while(getchar()!='\n')
continue;
switch(ch)
{
case 'a':
output(array,LIM);
break;
case 'b':
s_a(array,LIM);
break;
case 'c':
s_l(array,LIM);
break;
case 'd':
s_l_f(array,LIM);
break;
case 'q':
goto bye;
}
}
bye:puts("Bye!");
system("pause");
return 0;
}
int read(char (*p)[SIZE],int n)
{
int count=0;
int size;
puts("Please input up to 5 lines.");
puts("I will handle them.");
puts("Ctrl+z to quit.");
while(count
size=strlen(p[count]);
if(p[count][size-1]=='\n')
{
p[count][size-1]='\0'弯此;
}
else
{
p[count][size-1]='\陆闹乎0';
while(getchar()!='\n')
continue;
}
count++;
}
return count;
}
void d_m(void)
{
puts("Please select:");
puts("a.output strings according to the original order.");
puts("b.output strings sorted by ASCII.");
puts("c.output strings sorted by length.");
puts("d.output strings sorted by length of first word.");
puts("q.quit.");
}
void output(char (*p)[SIZE],int n)
{
int count;
for(count=0;count
}
void s_a(char (*p)[SIZE],int n)
{
int top,seek;
char *temp;
char *p1[LIM];
for(top=0;top
for(top=0;top
{
temp=p1[top];
p1[top]=p1[seek];
p1[seek]=temp;
}
for(top=0;top
}
void s_l(char (*p)[SIZE],int n)
{
int top,seek;
char *temp;
char *p1[LIM];
for(top=0;top
for(top=0;top
temp=p1[top];
p1[top]=p1[seek];
p1[seek]=temp;
}
for(top=0;top
}
void s_l_f(char (*p)[SIZE],int n)
{
int top,seek;
int array[n];
char *temp;
char *p2;
char *p1[LIM];
int temp1;
for(top=0;top
p1[top]=p[top];
array[top]=0;
}
for(top=0;top
if(isspace(p1[top][seek])||ispunct(p1[top][seek])||p1[top][seek]=='\0')
continue;
else
{
p2=&(p1[top][seek]);
while((isspace(*p2)==0)&&(ispunct(*p2)==0)&&(*p2!='\0'))
{
p2++;
array[top]++;
}
break;
}
}
for(top=0;top
temp=p1[top];
p1[top]=p1[seek];
p1[seek]=temp;
temp1=array[top];
array[top]=array[seek];
array[seek]=temp1;
}
for(top=0;top
puts(p1[top]);
}
}
#include
#include
#include
int main( void )
{
char input[81];
char *str[5];
char *p;
int i, j;
printf("Input 5 strings:\n");
for( i = 0; i < 5; i++ ){
gets( input );
str[i] =(char *)malloc( strlen(input)+1);
strcpy( str[i], input );
}
for( i = 0; i < 4; i++ )
for( j 枣郑= i + 1; j < 行烂5; j++ )
if( strcmp( str[i], str[j] ) > 0 ){
p = str[i];
str[i] = str[j];
str[j] = p;
}
for( i = 0; i < 5; i++ ){
printf( "%s\n", str[i] 档岩漏);
free( str[i]);
}
return 0;
}
#include
#include
int main()
{
char a[4][80];
printf("Input 5 strings:\n");
// 输入5个字符串
for(int i=0;i<5;i++)
{
gets(a[i]);
}
printf("---------------------------\n"饥蠢册);
// 排序
for(int i=0;i<4;i++)
{
for(int j=i+1;j<5;j++)
{
// 如果 i 串 大于 j 串,则交换烂宏
if( strcmp(a[i],a[j]) > 0)
{
char temp[80];
strncpy(temp, a[i],80);
temp[strlen(a[i])]='\0';
strncpy(a[i], a[j],80);
a[i][strlen(a[j])]='\0';
strncpy(a[j], temp,80);
a[j][strlen(temp)]='\0';
}
}
}
//档没 输出
for(int i=0;i<5;i++)
{
printf("%s\n",a[i]);
}
return 0;
}
来 看看
#include<岁穗stdio.h>
#include
#include
#define N 80
void test(void)
{
char *p[5],*q;
int i = 0,j = 0;
printf("input 5 strings:\渗行n");
for(i = 0 ; i <5; i++)
{
p[i] = (char *)malloc(sizeof(char)*N);
if(!p[i])
{
printf("p[%d]apply error\n",i);
exit(-1);
}
memset(p[i], '\0', N);
gets(p[i]);
}
for(i = 0 ; i < 5-1; i++)
{
for(j = 0;j < 5-1-i;j++)
if(strcmp(p[j],p[j+1]) > 0)
{
q = p[j];
p[j] = p[j+1];
p[j+1] = q;
}
}
printf("乎喊卜---------------------------\n");
for(i= 0 ;i < 5; i++)
{
puts(p[i]);
free(p[i]);
p[i] = NULL;
}
}
int main(void)
{
test();
return 0;
}
已通过运行,放心使用。欢迎采纳,欢迎追问。
# include
# include
void main()
{char a[5][100],c[100];
int i,d;
printf("罩誉拦Input 5 strings:\n");
for(i=0;i<5;i++)
gets(a[i]);
printf("虚燃-------------------\n");
for(i=0;i<5;i++)
for(d=i;d<4;d++)
{if(strcmp(a[d],a[d+1])==1)
{strcpy(c,a[d]);strcpy(a[d],a[d+1]);strcpy(a[d+1],c);}
}
for(i=0;i<物胡5;i++)
puts(a[i]);
}