C language programming> Twenty-first week ⑤ In the following given program, the function of the function fun is: use the bubble method to sort the 5 character strings from small to large.

Example: In the following given program, the function of the function fun is to sort the 5 character strings from small to large using the bubble method.

注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。

代码如下:

#include<stdio.h>
#include<string.h>
#define MAX 20
void fun(char*str[5])
{
    
    
	int i,j;
	char*p;
	for(i=0;i<5;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;
			}
		}
	}
}
main()
{
    
    
	int i;
	char*str[5],s[5][MAX];
	for(i=0;i<5;i++)
		str[i]=s[i];
	printf("\nEnter 5 sting(1 sting at each line):\n");
	for(i=0;i<5;i++)
		scanf("%s",str[i]);
	fun(str);
	printf("The strings after sorting:\n");
	for(i=0;i<5;i++)
		printf("%s  ",str[i]);
	printf("\n");
}

The output running window is as follows:
Insert picture description here

越努力越幸运!
加油,奥力给!!!

Guess you like

Origin blog.csdn.net/qq_45385706/article/details/112800396