#include<stdio.h> #include<string.h> int main() { int i,j,len,maxlen = - 1,minlen = 100,maxst,minst; char s[20010]; gets(s); len = strlen(s); i = 0; while(1); { while(i <= len && (s[i] == ' ' || s[i] == ',')) { i ++; if(i > len) { break; } } i = j; while(j <= len && !(s[i] == ' ' || s[i] == ',')) { j ++; } if(j - i > maxlen) { maxlen = j - i; maxst = i; } if(j - i < minlen) { minlen = j - i; minst = i; } i = j; } for(i = 0; i < maxlen; i ++) { printf("%c\n",s[maxst + i]); } for(i = 0; i < minlen; i ++) { printf("%c\n",s[minst + i]); } return 0; }这段代码哪里有问题
时间: 2024-01-18 22:03:03 浏览: 23
这段代码存在以下问题:
1. 在 `while(1)` 循环中,缺少结束循环的条件语句,导致程序会一直死循环。
2. 在第10行代码中,`i = j` 应该改为 `j = i`。
3. 在第13行代码中,应该是 `s[i] == ' ' || s[i] == ','` 而不是 `s[i] == ' ' || s[j] == ','`。
4. 在第15行代码中,应该是 `j = i` 而不是 `i = j`。
5. 在第18行代码中,应该是 `j - i < minlen` 而不是 `j - i > minlen`。
6. 在第30行代码中,应该是 `printf("%c",s[maxst + i])` 而不是 `printf("%c\n",s[maxst + i])`,因为这样会每个字符占一行输出。
综上所述,修改后的代码如下:
```
#include<stdio.h>
#include<string.h>
int main() {
int i = 0, j = 0, len, maxlen = -1, minlen = 100, maxst, minst;
char s[20010];
gets(s);
len = strlen(s);
while(i <= len) {
while(i <= len && (s[i] == ' ' || s[i] == ',')) {
i ++;
}
j = i;
while(j <= len && !(s[j] == ' ' || s[j] == ',')) {
j ++;
}
if(j - i > maxlen) {
maxlen = j - i;
maxst = i;
}
if(j - i < minlen) {
minlen = j - i;
minst = i;
}
i = j;
}
for(i = 0; i < maxlen; i ++) {
printf("%c",s[maxst + i]);
}
printf("\n");
for(i = 0; i < minlen; i ++) {
printf("%c",s[minst + i]);
}
printf("\n");
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)