几组字符串进行排序后输出

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

void main()
{
	char str[100][50]; 
	char c;
	int i=0,j=0,m,n;
	void sorted(char array[][50],int n);
	
	printf("输入字符串(以#结束),每行不超过50个字符");
	
	c = getchar();
	if(c == '#')
	printf("数组中没有字符串,无法进行排序");
	else
	{
	 while(c != '#')
	 {
	 	if(c!='\n')
		{
		  str[i][j++] = c;
		}
		else
		 {
		  str[i][j]='\0'; 
		  i++;
		  j = 0;
		 }
		c=getchar();
	 }
	 m = i;
	}
	sorted(str,m);
	
}
void sorted(char array[][50],int n)
{
	int i,j;
	char temp[50];
	
	for(i = 0;i < n;i++)
	{
	 for(j = i;j < n;j++)
	  {
	  	if(strcmp(array[i],array[j])>0)
    	{
    		strcpy(temp,array[i]);
    		strcpy(array[i],array[j]);
    		strcpy(array[j],temp);
		}
	  }
		
	}
	for(i = 0; i < n; i++)
	printf("%s\n",array[i]);
}

运行结果
在这里插入图片描述

发布了10 篇原创文章 · 获赞 0 · 访问量 1

猜你喜欢

转载自blog.csdn.net/qq_38272075/article/details/105528524