C 求字符数组最大值与次大值
2016-01-10 23:27
183 查看
实现代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXSIZE 100 void find_max(char*arr[], int size, char *max, char *second_max) { int i; for ( i = 0; i < size; i++) { if (strcmp(max, arr[i]) <= 0) { strcpy(second_max, max); strcpy(max, arr[i]); } if (strcmp(second_max, arr[i]) < 0 && strcmp(max, arr[i]) > 0) { strcpy(second_max, arr[i]); } } } void main() { char *arr[MAXSIZE]; char *max = NULL; char *second_max = NULL; int str_size; int i; max = (char*)malloc(sizeof(char)*MAXSIZE); second_max = (char*)malloc(sizeof(char)*MAXSIZE); max[0] = '\0'; second_max[0] = '\0'; printf("input string size:\n"); scanf("%d", &str_size); printf("input %d strings:\n", str_size); for (i = 0; i < str_size; i++) { arr[i] = (char*)malloc(sizeof(char)*MAXSIZE); fflush(stdin);//清除数据刚进来时缓冲区数据,防止被gets gets(arr[i]); } find_max(arr, str_size, max, second_max); printf("max = %s, second_max = %s\n", max, second_max); system("pause"); }
相关文章推荐
- 软工课程总结
- SpringMVC的几种返回方式
- APP架构构思基本思路初稿
- c语言实现数组栈
- 学习之路
- 使用 Monit 监控PHP 服务中遇到的坑和解决办法
- 那些年我们一起做过的shell面试题(三)
- 数据结构_3:栈:STL
- 给编程初学者的干货:“秘籍”学好一本就不易
- android加密方式及原理
- 启动mysql 报错,,Starting MySQL.Manager of pid-file quit without updating fi[失败] (2011-10-28 12:30:56)转载▼
- 让人一用钟情的VS插件系列之一——Web Essentials(Web开发必备利器)
- 宽字符与窄字符的转换
- 深入理解 Javascript 面向对象编程
- Programming with ZooKeeper - A basic tutorial
- 2015总结与2016展望
- Spark部署配置
- 谓词函数
- shell---登录失败的IP添加到/etc/hosts.deny
- 正则表达式实现简单的邮箱格式验证