#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
int main(void)
{
char ch[100];
int i, j, len, maxlen, p;
i = len = maxlen = 0;
printf(“\n输入一串字符:”);
gets(ch);
do
{
j = i;
for (len = 0; ch[i] != ” “&&ch[i] != “\0”; i++)
{
len++;
}
if (len > maxlen)
{
maxlen = len;
p = j;
}
i++;
} while (ch[i] != “\0”);
printf(“\n”);
if (len == 0)
printf(“\n没有输入单词!”);
else
{
for (i = p; i < p + maxlen; i++)
printf(“%c”, ch[i]);
printf(“\n”);
}
system(“pause”);
return 0;
}
解决方案
80
} while (ch[i] != “\0”);
改成
} while (ch[i-1] != “\0”);
改成
} while (ch[i-1] != “\0”);
#include<stdio.h> #include<stdlib.h> #pragma warning(disable:4996) int main(void) { char ch[100]; int i, j, len, maxlen, p; i = len = maxlen = 0; printf("\n输入一串字符:"); gets(ch); do { j = i; for (len = 0; ch[i] != " "&&ch[i] != "\0"; i++) { len++; } if (len > maxlen) { maxlen = len; p = j; } i++; } while (ch[i-1] != "\0"); printf("\n"); if (len == 0) printf("\n没有输入单词!"); else { for (i = p; i < p + maxlen; i++) printf("%c", ch[i]); printf("\n"); } system("pause"); return 0; }