字符数组的最大值和次大值

3/3/2017来源:C/C++教程人气:1198

输入一个字符数组,输出其最大值和次大值

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 100
void big(char *arr[],int size,char *big1,char *big2)
{
	int i;
	for(i=0;i<size;i++)
	{
		if(strcmp(big1,arr[i])<=0)
		{
			strcpy(big2,big1);
			strcpy(big1,arr[i]);
		}else{
			if(strcmp(big2,arr[i])<=0)
				strcpy(big2,arr[i]);
		}
	}
}
int main()
{
	int i,size;
	char *arr[maxsize];
	char *big1=NULL,*big2=NULL;
	big1=(char*)malloc(sizeof(char)*maxsize);
	big2=(char*)malloc(sizeof(char)*maxsize);
	big1[0]='\0';
	big2[0]='\0';
	PRintf("size=");
	scanf("%d",&size);
	for(i=0;i<size;i++)
	{
		arr[i]=(char*)malloc(sizeof(char)*maxsize);
		fflush(stdin);
		gets(arr[i]);
	}
	big(arr,size,big1,big2);
	printf("the biggest str is:%s\n",big1);
	printf("the second biggest str is:%s\n",big2);
	free(big1);
	free(big2);
	for(i=0;i<size;i++)
		free(arr[i]);
	system("pause");
	return 0;
}