解题思路:

1、字符串比较大小的函数strcmp(char *a, char *b)

    a) a == b 返回0

    b)a < b 返回负值

    c) a > b返回正值

2、字符串拷贝函数 strcpy(char *a, char *b),将字符串b拷贝到字符串a中。

3、既然是排序问题,那么就可以用冒泡排序法。

注意事项:

参考代码:

#include<stdio.h>
#include<string.h>

int main()
{
	char a[3][128], tmp[128];
	int i, j;
	for(i=0; i<3; i++)
	{
		scanf("%s", a[i]);
	}
	for(i=0; i<3; i++)
	{
		for(j=i+1; j<3; j++)
		{
			if(strcmp(a[i], a[j]) > 0)
			{
				memset(tmp, 0, sizeof(tmp));
				strcpy(tmp, a[i]);
				strcpy(a[i], a[j]);
				strcpy(a[j], tmp);
			}
		}
	}
	for(i=0; i<3; i++)
	{
		printf("%s\n", a[i]);
	}
	return 0;
}


 

0.0分

76 人评分

  评论区

这是选择排序
2021-03-05 17:40:23
单纯想用函数调用
#include <stdio.h>
#include <string.h>
#define N 100
char ch[3][N];

void bubble(char ch[][N],int i);

int main(int argc,int const* argv[])
{
	int i=0;
	for(i=0;i<3;i++)
	{
	scanf("%s",ch[i]);
		}
		bubble(ch,3);
		for(int m=0;m<3;m++)
		printf("%s\n",ch[m]);
	return 0;
 } 
void bubble(char ch[][N],int i)
{
	char character[128];
	for(int j=0;j<i;j++)
	{
		for(int k=j+1;k<i;k++)
		if(strcmp(ch[j],ch[k])>0)
		{
			memset(character, 0, sizeof(character));
                strcpy(character, ch[j]);
                strcpy(ch[j], ch[k]);
                strcpy(ch[k], character);
		}
	}		
}
2021-02-06 21:07:07
求解memset函数在这里的用法
2021-01-29 09:58:01
# include <stdio.h>
# include <string.h>
int main(void)
{char a[128],b[128],c[128],d[128],e[128],f[128];
	gets(a);gets(b);gets(c);
	if(strcmp(a, b)>0)
	{strcpy(f, a); strcpy(e, b);}	
	else
	{strcpy(f, b); strcpy(e, a);}
	if(strcmp(f, c)>0 && strcmp(a, b)>0)
		strcpy(d, c);			
	else if(strcmp(f, c)>0 && strcmp(a, b)<0)
		strcpy(d, c);			
	else if(strcmp(f, c)<0 && strcmp(a, b)>0)
	{strcpy(f, c); strcpy(d, b); strcpy(e, a);}
	else
	{strcpy(f, c); strcpy(e, b); strcpy(d, a);}
	printf("%s\n", d); printf("%s\n", e); printf("%s\n", f);									
	return 0;		
}
竟然过了
2020-12-10 10:13:09
老师这题用字典序可以做吗
2020-12-09 19:49:03
呜呜呜呜呜,大佬救命啊,我到底错在哪里了
#include<stdio.h>
#include<string.h>
int main()
{
	char a[128],b[128],c[128],temp[128];
	scanf("%s",&a);
	scanf("%s",&b);
	scanf("%s",&c);
	if(strcmp(a,b)){
		strcpy(temp,a);
		strcpy(a,b);
		strcpy(b,temp);
	}
	if(strcmp(b,c)){
		strcpy(temp,c);
		strcpy(c,b);
		strcpy(b,temp);
	}
	if(strcmp(a,b)){
		strcpy(temp,a);
		strcpy(a,b);
		strcpy(b,temp);
	}
	puts(a);
	puts(b);
	puts(c);
	return 0;
}
2020-03-26 23:02:52
傻傻的做法
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	string str1,str2,str3,a,b,c,t;
	getline(cin,str1);
	getline(cin,str2);
	getline(cin,str3);
	for(int i=0;i<str1.length();i++)
    {
        a+=str1[i];
    }
    for(int i=0;i<str2.length();i++)
    {
        b+=str2[i];
    }
    for(int i=0;i<str3.length();i++)
    {
        c+=str3[i];
    }
if(a>b)
    {
        t=b;b=a;a=t;
    }
    if(b>c)
    {
        t=c;c=b;b=t;
    }
    if(a>b)
    {
        t=b;b=a;a=t;
    }
    cout<<a<<endl;cout<<b<<endl;cout<<c<<endl;
	return 0;
 }
2020-03-13 21:07:55
#include<stdio.h>
#include<string.h>
int main()
{
	char b[3][4], t;
	for (int i = 0; i < 3; i++)
		gets(b[i]);
	for (int i = 0; i < 3; i++)
	{
		if (b[i][0] > b[i][1])
		{  
			t = b[i][0];
			b[i][0] = b[i][1];
			b[i][1] = t;
		}
		if (b[i][1] > b[i][2])
		{
			t = b[i][1];
			b[i][1] = b[i][2];
			b[i][2] = t;
			if (b[i][0] > b[i][1])
			{
				t = b[i][0];
				b[i][0] = b[i][1];
				b[i][1] = t;
			}
		}
	}
	for (int i = 0; i < 3; i++)
	{
		if (b[0][i] > b[1][i])
		{
			t = b[0][i];
			b[0][i] = b[1][i];
			b[1][i] = t;
		}
		if (b[1][i] > b[2][i])
		{
			t = b[1][i];
			b[1][i] = b[2][i];
2020-02-05 15:48:27