首页 > 试题广场 >

输入3个字符串,按由小到大的顺序输出。要求用指针方法处理。

[问答题]

输入3个字符串,按由小到大的顺序输出。要求用指针方法处理。

推荐

#include<stdioh>

#include<string.h>

int main()

{void swap(char *,char*);

char str1[20],str2[20],str3[20];

printf("input three line:\n'");

gets(str1);

gets(str2);

gets(str3);

if(strcmp(str1,str2)>0)swap(str1,str2);

if(strcmp(str1,str3)>0)swap(str1,str3);

if(strcmp(str2,str3)>0)swap(str2,str3);

printf("Now,the orderis:\n”);

printf("%s\n%s\n%s\n",str1,str2,str3);

return 0;

}


void swap(char *p1,char *p2)

{char p[20]:

strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);

}


发表于 2018-03-25 10:31:23 回复(0)
# include<stdio.h>
#include<string.h>
int main()
{
char str1[10],str2[20],str0[10];
printf("please input 3 strings");
gets(str1);
gets(str2);
gets(str0);
if(strcmp(str1,str2)>0) swap(str1,str2);/*字符串比较函数*/
if(strcmp(str2,str0)>0) swap(str2,str0);
if(strcmp(str1,str0)>0)  swap(str1,str0);
printf("Now the otrder is:")
printf("%s\n%s\n%s"\nstr1,str2,str0);

return 0;
}
void swap(char *p1,*p2)
{
char str[10];
strcpy(str,p1);
strcpy(p1,p2);
strcpy(p2,str);
}

发表于 2018-05-20 20:50:22 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(void)
{
	char *str[80][80] , temp[80];
	int n, i, j;

	printf("请输入n:");

	scanf("%d", &n);

	for (i = 0; i < n; i++)
		scanf("%s", str[i]);

	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - 1 - i; j++)
		{
			if (strcmp(str[j] , str[j + 1]) > 0)
			{
				strcpy(temp, str[j]);
				strcpy(str[j], str[j+1]);
				strcpy(str[j+1], temp);
			}
		}
	}

	printf("after soft:\n");


	for (i = 0; i < n; i++)
	{
		printf("%s ", str[i]);
	}


	return 0;
}

发表于 2020-07-03 15:57:08 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void sort(char * group[]){
 for(int i = 0 ; i < 3; i ++){
  for(int j = 0; j < 2; j ++){
   if(strcmp(group[j],group[j + 1]) > 0){
    char* trans = group[j];
    group[j] = group[j + 1];
    group[j + 1] = trans;
   }
  }
 }
}
int main(){
 char* group[3];
 for(int i = 0; i < 3; i ++){
  group[i] = malloc(sizeof(char) * 100);
 }
 for(int i = 0; i < 3; i ++){
  gets(group[i]);
 }
 sort(group);
 for(int i = 0; i < 3; i ++){
  puts(group[i]);
 }
}
发表于 2020-01-02 13:16:03 回复(0)
链接:https://www.nowcoder.com/questionTerminal/fb83d4701a204a9fa63c11fd0533bc32
来源:牛客网
#include<stdioh>
 
#include<string.h>
 
intmain()
 
{voidswap(char*,char*);
 
charstr1[20],str2[20],str3[20];
 
printf("input three line:\n'");
 
gets(str1);
 
gets(str2);
 
gets(str3);
 
if(strcmp(str1,str2)>0)swap(str1,str2);
 
if(strcmp(str1,str3)>0)swap(str1,str3);
 
if(strcmp(str2,str3)>0)swap(str2,str3);
 
printf("Now,the orderis:\n”);
 
printf("%s\n%s\n%s\n",str1,str2,str3);
 
return0;
 
}
 
 
 
voidswap(char*p1,char*p2)
 
{charp[20];
 
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
 
}
发表于 2019-11-14 21:05:34 回复(0)