编程在一个已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。
char p[1000];
printf("请输入字符串:");
fgets(p, 1000, stdin);
int i = 0;
int high = 0;
int low = 0;
int low_temp = 0;
int high_temp = 0;
int count = 0;
int temp = 0;
for (i = 0; i < strlen(p); i++) {
temp = 0;
low_temp = i;
while (p[i] != ‘ ‘ && p[i] != ‘\0‘) {
temp++;
i++;
}
high_temp = i-1;
if (temp > count ) {
count = temp;
low = low_temp;
high = high_temp;
}
}
for (int i = low; i <= high; i++) {
printf("%c",p[i]);
}
printf("\n");
时间: 2024-10-17 20:41:48