|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 coura 于 2021-10-10 10:13 编辑
大神,我的大神,嘿嘿
题目参见S1E20二维数组第2题
- #include <stdio.h>
- #include <string.h>
- #define NUM 5
- int main()
- {
- char slogan[NUM][100];
- int i, j, ch, min, max, temp;
- for (i = 0; i < NUM; i++)
- {
- printf("请输入%d句话:", i+1);
- for (j = 0; (ch = getchar()) != '\n'; j++)
- {
- slogan[i][j] = ch;
- }
- slogan[i][j] = '\0';
- }
- min = 0;
- max = min;
- printf("你输入了下边%d句话:\n", NUM);
- // 打印每句口号,同时比较长度
- for (i = 0; i < NUM; i++)
- {
- printf("%s\n", slogan[i]);
- temp = strlen(slogan[i]);
- min = temp < strlen(slogan[min]) ? i : min;
- max = temp > strlen(slogan[max]) ? i : max;
- }
- printf("其中最长的是:%s\n", slogan[max]);
- printf("其中最短的是:%s\n", slogan[min]);
- return 0;
- }
复制代码
temp的引用是在这里
- for (i = 0; i < NUM; i++)
- {
- printf("%s\n", slogan[i]);
- temp = strlen(slogan[i]);
- min = temp < strlen(slogan[min]) ? i : min;
- max = temp > strlen(slogan[max]) ? i : max;
-
复制代码
temp = strlen(slogan[i]);
strlen -> 计算字符串长度
slogan[i] -> 字符串,
所以 temp = strlen(slogan[i]); -> 计算字符串长度
后面的两句就是比较最大最小了
|
|