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

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

输入一个字符数组,确定其长度的最大值和次大值

#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(strlen(big1)<strlen(arr[i]))
		{
			strcpy(big2,big1);
			strcpy(big1,arr[i]);
		}else{
			if(strlen(arr[i])>strlen(big2))
				strcpy(big2,arr[i]);
		}
	}
}

int main(int argc,char *argv[])
{

	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; 
}