C语言打印最长字符串

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