#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
void input_char(char *str) {
int i, p=0;
while (1)
{
scanf("%s", str);
if (strlen(str) > 8) {
printf("输入错误,字符串长度超过了8个字符,请重新输入!\n");
}
else p++;
for (i = 0; i < 8; i++)
{
if (str[i] == " ")
{
printf("输入错误,字符串不能有空格,请重新输入!\n");
p++;
}
}
if (p == 9)break;
}
void max_strings(char *str1, char *str2, char *str3) {
if (strcmp(str1, str2) >= 0 && strcmp(str1, str3) >= 0) {
printf("最大的字符串是:%s\n", str1);
}
else if (strcmp(str2, str1) >= 0 && strcmp(str2, str3) >= 0) {
printf("最大的字符串是:%s\n", str2);
}
else {
printf("最大的字符串是:%s\n", str3);
}
}
int main() {
char string1[9];
char string2[9];
char string3[9];
input_char(string1);
input_char(string2);
input_char(string3);
max_strings(string1, string2, string3);
return 0;
}找错
I've reviewed the code, and there are several issues that need to be addressed. Here's a list of the errors and suggestions for fixing them:
In the input_char function:
} at the end.if (str[i] == " ") is incorrect. It should be if (str[i] == ' ') (single quotes for a character).p variable is not being reset between iterations, which may cause unexpected behavior.if (p == 9) will never be reached as p is only incremented to a maximum of 2.In the max_strings function:
In the main function:
Here's the corrected input_char function:
void input_char(char *str) {
int i, p = 0;
while (1) {
scanf("%s", str);
if (strlen(str) > 8) {
printf("输入错误,字符串长度超过了8个字符,请重新输入!\n");
continue;
}
p = 0;
for (i = 0; i < strlen(str); i++) {
if (str[i] == ' ') {
printf("输入错误,字符串不能有空格,请重新输入!\n");
p = 1;
break;
}
}
if (p == 0) break;
}
}These changes should resolve the issues in the code. The main problems were in the input_char function, which had logical errors and syntax issues. The corrected version should properly handle input validation for string length and spaces.